Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save image URL to photo library and subsequently use the saved image

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?

like image 493
Zhen Avatar asked Dec 16 '22 08:12

Zhen


1 Answers

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 
}
like image 56
Amar Kulo Avatar answered May 07 '23 08:05

Amar Kulo