Thumbnail from NSURL - objective-c

I am loading files that are downloaded from the web in a UIWebView (HTML files).
Is there a way to create a UIImage from an HTML file located in the Documents folder?
The goal is to auto generate thumbnails without needing to manual create them...
Update
My train of thought would be to do something in the line of:
NSString * s = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"];
UIImage *image = [[UIImage alloc] initWithData:
[NSData dataWithContentsOfURL:url]];
Which of course does not work in this case!
Thanks!
S.

I found this solution here
UIGraphicsBeginImageContext(yourWebView.bounds.size);
[yourWebView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultImageView = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
resultImageView will be an image that contains the render of yourWebView.
If you want to shrink it down to a thumbnail size from there, I like Trevor's UIImage category for that purpose.

Related

How to compose an image from GUI elements on iOS?

I need to form an image by composing some visual elements and save it on disk. The question is: how to "screenshot" a certain area of a view? Possibly a view that is not visible, so the procedure can be executed unnoticed?
This code snippet shows how to render a view to a UIImage:
UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
And this snippet shows how to save a UIImage as a JPEG or a PNG:
NSString *pngPath = [NSHomeDirectory()
stringByAppendingPathComponent:#"Documents/Test.png"];
NSString *jpgPath = [NSHomeDirectory()
stringByAppendingPathComponent:#"Documents/Test.jpg"];
[UIImageJPEGRepresentation(viewImage, 1.0) writeToFile:jpgPath atomically:YES];
[UIImagePNGRepresentation(viewImage) writeToFile:pngPath atomically:YES];

UIImage initWithContentsOfFile doesn't work

I have question: I want to avoid [UIImage imageNamed:].
So i did:
UIImage *prodImg = [[UIImage alloc] initWithContentsOfFile:#"myimage.png"];
controller.productImg.image = prodImg;
[prodImg release];
But now the image is not shown anymore.
Does anyone know how to get that to work?
The above code is not working because correct way to do it is-
NSString *thePath = [[NSBundle mainBundle] pathForResource:#"default" ofType:#"jpeg"];
UIImage *prodImg = [UIImage imageWithContentsOfFile:thePath];
controller.productImg.image = prodImg;
[prodImg release];
;)

iOS load an image based on user input

fairly new to Objective-C and iOS development (coming from PHP) and I have a relatively simple question that I can't seem to find an answer to:
I am following along with an example for split View design where a web page is loaded into the Detail View when a user clicks an item in the master view. I got all this working, but would like to substitute web view for an image. So I've amended the app to load a UIImage instead of a WebView. What I'm looking for is the equivalent to this code:
NSString *urlString = [pagesAddress objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:urlString];
// these 2 is where I get lost with the images.
NSURLRequest = *request = [NSURLRequest requestWithURL:url];
[detailViewController.webView loadRequest:request];
I came up with this:
NSString *imageName = [pagesAddress objectAtIndex:indexPath.row];
UIImage *myImage = [UIImage imageNamed:imageName];
// missing the last 2 calls: one to tell Xcode that it's an image "request" I want and the second to load the actual image (based on it's name that is already in an array) into the ImageView.
Thanks.
PS
I tried this:
NSString *imageName = [pagesAddress objectAtIndex:indexPath .row];
[detailViewController.imageView setImage:[UIImage imageNamed:imageName]];
And it shows just the first image, then crashes when I try to show the last one.
In the end, the solution were those 2 lines when I amended the code:
NSString *imageName = [pagesAddress objectAtIndex:indexPath.row];
[detailViewController.imageView setImage:[UIImage imageNamed:imageName]];
Notice that I had to change the setImage to convert the NSString to a UIImage or Xcode would complain. It turns out it was crashing because in the array where I had the image names, I had put 3 images into one entry (basically I forgot the commas!) so it was out of range.
Tim:
This line you gave me
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
is unnecessary because I already have a view created, it would create another view which I never used. Also, replacing it with CGRect seems overkill if I already have a UIImage placeholder no?
In any case, it works now and I'm very grateful for all the help. iPad development with Objectve-C is a very thorny road and I expect I'll be bugging you guys some more.
Cheers.
Try this:
UIImage *myImage = [[UIImage alloc] initWithData:[NSData dataWithConentsOfURL:[NSURL URLWithString:urlString];
// don't know if you already got the following?
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
[imageView setImage:myImage];
[self.view addSubview:imageView];
The first line is synchronous (= blocking), so in production, you should rather use - [NSURLRequest start] for this (but that's a bit more complicated).
Or use this for your local images:
UIImage *myImage = [UIImage imageNamed:imageName];
// Now, follow the same steps as in the first code-example, just skip the first line.
Try this (on iOS 4.0 and later):
// Execute a block of code on a background thread.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^(void)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
// When IO is done and image created, set it on the main thread.
dispatch_async(dispatch_get_main_queue(),
^(void)
{
imageView.image = image;
});
[pool release];
});

How to display images from plist in cocoa mac

I am developing mac desktop app using objective c. i have array of images to display one after one when button pressed, so i am want to use plist to store image names and i want to display image on image view using plist.
Just load your plist where you stored you image's paths to an array like this :
NSString *myfile = [[NSBundle mainBundle]
pathForResource:#"images" ofType:#"plist"];
imagesArray = [[NSArray alloc] initWithContentsOfFile:myfile];
and then when you want to display an image, juste get it's path from the array above, example :
UIImage *img = [UIImage imageNamed: [imagesArray objectAtIndex:4]];
myImageView.image = img;

iPhone SDK and Multipage tiff

I need to access the iphone APIs to read a multipage TIFF image file format, dose anyone know how:, if not how can I build and use the open source libtiff on iphone/xcode.
I've created my own solution for this: NSTiffSplitter.
You can find it on github.
NSString *pathToImage = [[NSBundle mainBundle] pathForResource:#"Example" ofType:#"tiff"];
NSTiffSplitter* splitter = [[NSTiffSplitter alloc] initWithPathToImage:pathToImage];
UIImage *page = [[UIImage alloc] initWithData:[splitter dataForImage:page]];
yourImageView.image = page;
[page release];