2012-07-21 160 views

回答

115

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

核心数据与顺便将图像保存到磁盘无关。

+0

通过使用UIImagePNGRepresentation失去所有的方位信息。 – 2017-02-08 07:42:27

+1

那么如何保存图像而不会丢失信息? – Pol 2017-04-28 08:48:24

3

首先,你应该得到的文档目录

/* 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]; 
6
NSData *imageData = UIImagePNGRepresentation(image); 
[imageData writeToFile:path atomically:YES]; 

其中路径是你想要写它的文件名至。

14

以上是有用的,但它们不回答你如何保存在子目录或从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图像,la ST 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]; 
25

在斯威夫特3:

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

// Save image. 
UIImagePNGRepresentation(image)?.writeToFile(filePath, atomically: true) 
+1

不再有效 - 你需要使用'.write()throws' – 2017-04-26 19:17:16

9
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")