You can even save the image to the bank, but it is not usual. Generally what you do is save the image in the sandbox of the application (the disk area to which only the app has access). What you need to save, using a database for example, is the path of the image.
You can download the image as follows:
- (UIImage *)loadImageFromURL:(NSString *)fileURL {
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
return [UIImage imageWithData:data];
}
So, if you want to save the UIImage
object to disk, convert it to NSData
:
- (void)saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
NSData *imageData;
if ([[extension lowercaseString] isEqualToString:@"png"]) {
imageData = UIImagePNGRepresentation(image);
extension = @"png";
} else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
imageData = UIImageJPEGRepresentation(image, 1.0);
extension = @"jpg";
}
if (imageData) {
NSString *imagePath = [directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, extension]];
[imageData writeToFile:imagePath options:NSAtomicWrite error:nil];
}
}