How do I save a UIImage to a file?

前端 未结 9 880
抹茶落季
抹茶落季 2020-12-07 13:13

If I have a UIImage from an imagePicker, how can I save it to a subfolder in the documents directory?

9条回答
  •  借酒劲吻你
    2020-12-07 13:20

    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.

提交回复
热议问题