Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Download image from url and save in device

I am trying to download the image from the url http://a3.twimg.com/profile_images/414797877/05052008321_bigger.jpg

I am using the following code, but image is not saved in the device. I want to know what I am doing wrong.

 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://a3.twimg.com/profile_images/414797877/05052008321_bigger.jpg"]];  [NSURLConnection connectionWithRequest:request delegate:self];   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  NSString *documentsDirectory = [paths objectAtIndex:0];  NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"pkm.jpg"];  NSData *thedata = NULL;  [thedata writeToFile:localFilePath atomically:YES];   UIImage *img = [[UIImage alloc] initWithData:thedata]; 
like image 672
User97693321 Avatar asked Mar 23 '10 10:03

User97693321


People also ask

How do I download an image from a URL?

Click on the Download Image from URL button, the field will appear on the right. Enter the full web address of the image. Click on the arrow to the right of the field and select the Force Check checkbox. Then click the Save button.

How do I save an image from URL on iPad?

Place your finger on the photo and hold it on the image until a menu appears on the screen. Tap Save Photo (or Save Image or Add to Photos depending on the app) to download it. In Safari, the menu may include options such as Open in a New Tab or Add to Reading List when the image is also a link to another web page.

How do I download an image from Swift 5?

Downloading an Image From a URL Let's start with a blank Xcode project. Select New > Project... from Xcode's File menu and choose the Single View App template from the iOS > Application section. Name the project Images and set User Interface to Storyboard. Leave the checkboxes at the bottom unchecked.


2 Answers

I happen to have exactly what you are looking for.

Get Image From URL

-(UIImage *) getImageFromURL:(NSString *)fileURL {     UIImage * result;      NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];     result = [UIImage imageWithData:data];      return result; } 

Save Image

-(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {     if ([[extension lowercaseString] isEqualToString:@"png"]) {         [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];     } else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {         [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];     } else {         NSLog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);     } } 

Load Image

-(UIImage *) loadImage:(NSString *)fileName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {     UIImage * result = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.%@", directoryPath, fileName, extension]];      return result; } 

How-To

//Definitions NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];  //Get Image From URL UIImage * imageFromURL = [self getImageFromURL:@"http://www.yourdomain.com/yourImage.png"];  //Save Image to Directory [self saveImage:imageFromURL withFileName:@"My Image" ofType:@"png" inDirectory:documentsDirectoryPath];  //Load Image From Directory UIImage * imageFromWeb = [self loadImage:@"My Image" ofType:@"png" inDirectory:documentsDirectoryPath]; 
like image 104
Fernando Cervantes Avatar answered Sep 21 '22 00:09

Fernando Cervantes


If you set theData to nil, what do you expect it to write to the disk?

What you can use is NSData* theData = [NSData dataWithContentsOfURL:yourURLHere]; to load the data from the disk and then save it using writeToFile:atomically:. If you need more control over the loading process or have it in background, look at the documentation of NSURLConnection and the associated guide.

like image 21
Alfonso Avatar answered Sep 20 '22 00:09

Alfonso