iPhone - UIImagePickerController - >将图像保存到app文件夹

时间:2011-02-10 13:57:56

标签: iphone objective-c uiimage uiimagepickercontroller

我有一个使用UIImagePickerController的iPhone应用程序。正如sourceType我有

  • UIImagePickerControllerSourceTypePhotoLibrary
  • UIImagePickerControllerSourceTypeCamera
  • UIImagePickerControllerSourceTypeSavedPhotosAlbum

因此用户可以从照片或相机照片中制作照片或从照片库中选择一张照片。

图像将以UIImageView显示。如果用户关闭应用程序,则应保存图像。因此,对于文本字段,我使用NSUserDefaults。我知道,这不是用NSData在NSUSerDefaults中保存图像的好方法,所以我想将图像保存/复制到由我的应用程序控制的文件夹,类似于NSUserDefaults。

我该怎么做?我想保存它,然后我可以将文件的路径写入我的NSUserDefaults并在启动应用程序时读取它。

提前谢谢你&最诚挚的问候。

4 个答案:

答案 0 :(得分:48)

您可以在UIImagePickerControllerDelegate委托实施

中使用以下代码
- (void) imagePickerController:(UIImagePickerController *)picker
 didFinishPickingMediaWithInfo:(NSDictionary *)info {

    //obtaining saving path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo.png"];

    //extracting image from the picker and saving it
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];   
    if ([mediaType isEqualToString:@"public.image"]){
        UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
        NSData *webData = UIImagePNGRepresentation(editedImage);
        [webData writeToFile:imagePath atomically:YES];
    }
}

这就是全部

答案 1 :(得分:8)

根据您要保存的文件格式,您可以使用

[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];

OR

[UIImageJPEGRepresentation(image) writeToFile:path atomically:YES];

答案 2 :(得分:3)

这是将UIImage保存到文档目录中的代码。您可以在didFinishPickingImage委托方法中使用此代码:

// Create paths to output images
NSString  *pngPath = [NSHomeDirectory();
stringByAppendingPathComponent:@"Documents/Test.png"];
NSString  *jpgPath = [NSHomeDirectory();
stringByAppendingPathComponent:@"Documents/Test.jpg"];

// Write a UIImage to JPEG with minimum compression (best quality)
// The value 'image' must be a UIImage object
// The value '1.0' represents image compression quality as value from 0.0 to 1.0
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];

// Write image to PNG
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];

// Let's check to see if files were successfully written...

// Create file manager
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];

// Point to Document directory
NSString *documentsDirectory = [NSHomeDirectory();
stringByAppendingPathComponent:@"Documents"];

// Write out the contents of home directory to console
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
  

修改

您也可以使用:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

找到应用程序文档目录的路径,而不是NSHomeDierctory。

答案 3 :(得分:2)

更新knuku对Swift 3.0的回答

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    //obtaining saving path
    let fileManager = FileManager.default
    let documentsPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first
    let imagePath = documentsPath?.appendingPathComponent("image.jpg")

    // extract image from the picker and save it
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        try! UIImageJPEGRepresentation(pickedImage, 0.0)?.write(to: imagePath!)
    }
    self.dismiss(animated: true, completion: nil)        
}

此处图像保存为jpeg,但您也可以将其另存为png。 0.0参数代表压缩,如果你想最好地使用1.0,那么它是最低的质量。