How do I save a UIImage to a file?

前端 未结 9 910
抹茶落季
抹茶落季 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:34

    First you should get the Documents directory

    /* create path to cache directory inside the application's Documents directory */
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"fileName"];
    

    Then you should save the photo to the file

    NSData *photoData = UIImageJPEGRepresentation(photoImage, 1);
    [photoData writeToFile:filePath atomically:YES];
    

提交回复
热议问题