I am implementing a 'online image search' feature for my app. The flow that I require is as follows:
1) Retrieve a image url that the user wants to use
2) Save the image (via the URL) to the phone's photo album
3) Retrieve the saved image via the image picker controller and open up the move and scale screen
4) Use the image retrieved from the album.
Can anyone advise how I can do the steps above after obtaining the image URL?
You can save image in photo album with this code
UIImageWriteToSavedPhotosAlbum(yourImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error != NULL)
{
// handle error
}
else
{
// handle ok status
}
}
Now for executing code in another thread I would write code like this
// load data in new thread
[NSThread detachNewThreadSelector:@selector(downloadImage) toTarget:self withObject:nil];
This method you can have anywhere in your code, button or any other UIKit control. Then you will need methods that are going to do the hard work.
- (void)downloadImage
{
// network animation on
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
// create autorelease pool
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// save image from the web
UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"your_image_address.com"]]], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
[self performSelectorOnMainThread:@selector(imageDownloaded) withObject:nil waitUntilDone:NO ];
[pool drain];
}
- (void)imageDownloaded
{
// network animation off
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
// do whatever you need to do after
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With