How to download image from url and save it on my computer?

How to download an image from a URL and save it to a computer using Objective-C? This is what I got so far:

NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; UIImage *imageFromURL = [self getImageFromURL:@"https://www.google.com/images/srpr/logo11w.png"]; [self saveImage:imageFromURL withFileName:@"Google Logo" ofType:@"png" inDirectory:documentsDirectoryPath]; UIImage *imageFromWeb = [self loadImage:@"Google Logo" ofType:@"png" inDirectory:documentsDirectoryPath]; 

Xcode complains about UIIMage while trying to replace NSImage. He also complains about the undeclared self identifier.

I need to make an HTTP call to complete this. Explain it to me as if I were 5.

+6
source share
2 answers

Here is the code to save the image to the document directory.

 -(void)saveImagesInLocalDirectory { NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *imgName = @"image.png"; NSString *imgURL = @"www.example.com/image/image.png"; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *writablePath = [documentsDirectoryPath stringByAppendingPathComponent:imgName]; if(![fileManager fileExistsAtPath:writablePath]){ // file doesn't exist NSLog(@"file doesn't exist"); if (imgName) { //save Image From URL [self getImageFromURLAndSaveItToLocalData:imgName fileURL:imgURL inDirectory:documentsDirectoryPath]; } } else{ // file exist NSLog(@"file exist"); } } -(void) getImageFromURLAndSaveItToLocalData:(NSString *)imageName fileURL:(NSString *)fileURL inDirectory:(NSString *)directoryPath { NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]]; NSError *error = nil; [data writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", imageName]] options:NSAtomicWrite error:&error]; if (error) { NSLog(@"Error Writing File : %@",error); }else{ NSLog(@"Image %@ Saved SuccessFully",imageName); } } 

And this is the only code method.

 -(void)saveImagesInLocalDirectory { NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *imgName = @"image.png"; NSString *imgURL = @"www.example.com/image/image.png"; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *writablePath = [documentsDirectoryPath stringByAppendingPathComponent:imgName]; if(![fileManager fileExistsAtPath:writablePath]){ // file doesn't exist NSLog(@"file doesn't exist"); //save Image From URL NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString: imgURL]]; NSError *error = nil; [data writeToFile:[documentsDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", imgName]] options:NSAtomicWrite error:&error]; if (error) { NSLog(@"Error Writing File : %@",error); }else{ NSLog(@"Image %@ Saved SuccessFully",imgName); } } else{ // file exist NSLog(@"file exist"); } } 
+6
source

This is my decision!

 +(BOOL)downloadMedia :(NSString*)url_ :(NSString*)name{ NSString *stringURL = url_; NSURL *url = [NSURL URLWithString:stringURL]; NSData *urlData = [NSData dataWithContentsOfURL:url]; if ( urlData ) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,name]; [urlData writeToFile:filePath atomically:YES]; return YES; } return NO; } +(UIImage*)loadMedia :(NSString*)name{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:name]; UIImage *img_ = [UIImage imageWithContentsOfFile:getImagePath]; return img_; } 
0
source

Source: https://habr.com/ru/post/958122/


All Articles