NYC Tech Engineer NYC Tech Engineer - 5 months ago 11
Objective-C Question

How do I download an image from a URL and save it to my computer?

How would I download an image from a URL, and have that saved to the 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, trying to replace with NSImage. It also complains about an undeclared identifier 'self'.

I need to make an HTTP call to perform this as well. Explain this to me like I'm 5.

Answer

Here is the code to Save the image into document Directory.

-(void)saveImagesInLocalDirectory
{
        NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            NSString *imgName = @"image.png";
            NSString *imgURL = @"www.xxx.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 one method code..

-(void)saveImagesInLocalDirectory
{
        NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            NSString *imgName = @"image.png";
            NSString *imgURL = @"www.xxx.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");
            }
}