iphone learner
iphone learner

Reputation: 15

save images loaded from url in iphone

I have loaded images in UITableView from URLs. Now how can I save these images in my iPhone.

As you download an image, create a thumbnail of it with a proper naming convention and save it as well.

please provide some hint or methods for this saving images

Upvotes: 0

Views: 982

Answers (3)

dmmb
dmmb

Reputation: 21

NSURL *url = [[NSURL alloc] initWithString:@"http://image.com"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

more information about UIImageWriteToSavedPhotosAlbum you can find here

Upvotes: 2

Vikas S Singh
Vikas S Singh

Reputation: 1766

This is the code to download the image from image's weburl and save it inside the application.

NSURLRequest *request = [NSURLRequest requestWithURL:imageURL];
 [NSURLConnection connectionWithRequest:request delegate:self];

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"image.jpg"];
 NSData *thedata = [NSData dataWithContentsOfURL:imageURL];
 [thedata writeToFile:localFilePath atomically:YES];

Upvotes: 0

Nicolas Miari
Nicolas Miari

Reputation: 16256

// data is the NSData you obtained
// from the NSURLConnection

UIImage* image = [[UIImage alloc] initWithData:data];

// path is an NSString containing the path
// to save the file, usually inside Documents, Cache, etc,

[image writeToFile:path];

*Read Apple's docs on how to get the paths to relevant directories.

Upvotes: 0

Related Questions