Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have an iPhone application using a UIImagePickerController. As sourceType I have

  • UIImagePickerControllerSourceTypePhotoLibrary
  • UIImagePickerControllerSourceTypeCamera
  • UIImagePickerControllerSourceTypeSavedPhotosAlbum

so the user can make a photo or select one from the photo library from the photos or camera photos.

The image will be displayed in a UIImageView. The image should be saved if the user closes the app. So for text fields, I use NSUserDefaults. I know, it is not a good way to save the image inside the NSUSerDefaults with NSData, so I want to save / copy the image to a folder which is controlled by my application similar to the NSUserDefaults.

How can I do this? I want to save it, and then I can write the path to the file into my NSUserDefaults and read it on startup of the application.

Thank you in advance & Best Regards.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
466 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use the following code in UIImagePickerControllerDelegate delegate implementation

- (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];
    }
}

UPD

Swift 3.0 code by XueYu

here the image is saved as jpeg but you can also save it as png. the 0.0 parameter stands for compression and it's the lowest quality, if you want to get the best use 1.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)        
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...