113

如果我有一个UIImage来自 imagePicker 的文件,如何将其保存到文档目录中的子文件夹中?

4

9 回答 9

133

当然,您可以在应用程序的文档文件夹中创建子文件夹。你习惯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];

顺便说一句,Core Data 与将图像保存到磁盘无关。

于 2012-07-21T12:52:47.683 回答
26

在斯威夫特 3 中:

// Create path.
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let filePath = "\(paths[0])/MyImageName.png"

// Save image.
UIImagePNGRepresentation(image)?.writeToFile(filePath, atomically: true)
于 2015-12-29T01:30:10.450 回答
25

您必须将图像的表示构造为特定格式(例如 JPEG 或 PNG),然后调用writeToFile:atomically:该表示:

UIImage *image = ...;
NSString  *path = ...;
[UIImageJPEGRepresentation(image, 1.0) writeToFile:path atomically:YES];
于 2012-07-21T12:50:31.693 回答
18

以上内容很有用,但它们没有回答您关于如何保存在子目录中或从 UIImagePicker 获取图像的问题。

首先,您必须在 .m 或 .h 代码文件中指定您的控制器实现图像选择器的委托,例如:

@interface CameraViewController () <UIImagePickerControllerDelegate>

@end

然后实现委托的 imagePickerController:didFinishPickingMediaWithInfo: 方法,在这里你可以从图像选择器中获取照片并保存它(当然,你可能有另一个类/对象来处理保存,但我将只显示代码在方法内部):

- (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];
于 2013-10-02T23:48:39.493 回答
13
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")
于 2017-06-08T12:35:16.757 回答
6
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:path atomically:YES];

其中 path 是您要写入的文件的名称。

于 2012-07-21T12:54:55.053 回答
4

首先你应该得到 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];
于 2012-07-21T12:54:37.143 回答
3

在 Swift 4.2 中:

// Create path.
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
if let filePath = paths.first?.appendingPathComponent("MyImageName.png") {
    // Save image.
    do {
       try image.pngData()?.write(to: filePath, options: .atomic)
    } catch {
       // Handle the error
    }
}

于 2019-03-05T18:32:26.303 回答
1

在 Swift 4 中:

// Create path.
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
if let filePath = paths.first?.appendingPathComponent("MyImageName.png") {
    // Save image.
    do {
       try UIImagePNGRepresentation(image)?.write(to: filePath, options: .atomic)
    }
    catch {
       // Handle the error
    }
}
于 2018-05-31T00:55:20.350 回答