如何将UIImage保存到文件?
如果我有一个imagePicker的UIImage
,我怎样才能将其保存到文档目录中的子文件夹?
当然,你可以在应用程序的文档文件夹中创build子文件夹。 你使用NSFileManager
来做到这一点。
您使用UIImagePNGRepresentation
将您的图像转换为NSData并将其保存到磁盘。
// Create path. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Image.png"]; // Save image. [UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
核心数据与顺便将图像保存到磁盘无关。
在Swift 3:
// Create path. let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) let filePath = "\(paths[0])/MyImageName.png" // Save image. UIImagePNGRepresentation(image)?.writeToFile(filePath, atomically: true)
您必须将图像的表示forms作为特定的格式 (例如JPEG或PNG),然后调用writeToFile:atomically:
在表示上:
UIImage *image = ...; NSString *path = ...; [UIImageJPEGRepresentation(image, 1.0) writeToFile:path atomically:YES];
以上是有用的,但他们不回答你如何保存在一个子目录或从UIImagePicker获取图像的问题。
首先,您必须指定您的控制器在.m或.h代码文件中实现图像选取器的委托,例如:
@interface CameraViewController () <UIImagePickerControllerDelegate> @end
然后你实现委托的imagePickerController:didFinishPickingMediaWithInfo:方法,这是你可以从图像select器获取照片并保存它(当然,你可能有另一个类/对象来处理保存,但我只是显示代码在方法里面):
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { // get the captured image UIImage *image = (UIImage *)info[UIImagePickerControllerOriginalImage]; NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *imageSubdirectory = [documentsDirectory stringByAppendingPathComponent:@"MySubfolderName"]; NSString *filePath = [imageSubdirectory stringByAppendingPathComponent:@"MyImageName.png"]; // Convert UIImage object into NSData (a wrapper for a stream of bytes) formatted according to PNG spec NSData *imageData = UIImagePNGRepresentation(image); [imageData writeToFile:filePath atomically:YES]; }
如果你想保存为JPEG图像,最后3行将是:
NSString *filePath = [imageSubdirectory stringByAppendingPathComponent:@"MyImageName.jpg"]; // Convert UIImage object into NSData (a wrapper for a stream of bytes) formatted according to JPG spec NSData *imageData = UIImageJPEGRepresentation(image, 0.85f); // quality level 85% [imageData writeToFile:filePath atomically:YES];
NSData *imageData = UIImagePNGRepresentation(image); [imageData writeToFile:path atomically:YES];
其中path是要写入的文件的名称。
extension UIImage { /// Save PNG in the Documents directory func save(_ name: String) { let path: String = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! let url = URL(fileURLWithPath: path).appendingPathComponent(name) try! UIImagePNGRepresentation(self)?.write(to: url) print("saved image at \(url)") } } // Usage: Saves file in the Documents directory image.save("climate_model_2017.png")
首先你应该得到Documents目录
/* create path to cache directory inside the application's Documents directory */ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"fileName"];
那么你应该将照片保存到文件
NSData *photoData = UIImageJPEGRepresentation(photoImage, 1); [photoData writeToFile:filePath atomically:YES];