How do I save a UIImage to a file?
Of course you can create subfolders in the documents folder of your app. You use NSFileManager
to do that.
You use UIImagePNGRepresentation
to convert your image to NSData and save that to disk.
// Create path.NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Image.png"];// Save image.[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
Core Data has nothing to do with saving images to disk by the way.
In Swift 3:
// Create path.let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)let filePath = "\(paths[0])/MyImageName.png"// Save image.UIImagePNGRepresentation(image)?.writeToFile(filePath, atomically: true)
You have to construct a representation of your image as a particular format (say, JPEG or PNG), and then call writeToFile:atomically:
on the representation:
UIImage *image = ...;NSString *path = ...;[UIImageJPEGRepresentation(image, 1.0) writeToFile:path atomically:YES];