简体   繁体   中英

How to save image after selecting from UIImagepickercontroller

I'm building an iOS app using Swift and Xcode 6. I have implemented the functionality to pick image from UIImagePickerController and set into UIImageView.

Here is my to code select image from UIImagePickerController:

@IBAction func loadImageButtonTapped(sender: UIButton) {
        imagePicker.allowsEditing = false
        imagePicker.sourceType = .PhotoLibrary
        // imagePicker.sourceType = .Camera
        //imagePicker.sourceType = .SavedPhotosAlbum

        presentViewController(imagePicker, animated: true, completion: nil)
    }


    // MARK: - UIImagePickerControllerDelegate Methods
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
           // imageView.contentMode = .ScaleAspectFit
            imageView.image = pickedImage

        }

        dismissViewControllerAnimated(true, completion: nil)
    }

    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        dismissViewControllerAnimated(true, completion: nil)
    }

Now i'm finding a best way to store this image and when user come again in the app, i can get the image and display in the uiimageview.

Could some help me how can i implement this.

Help is appreciated!

Thanks in Advance!

Try this.

let imageData: NSData = UIImageJPEGRepresentation(yourImage, 0.9)
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let destinationPath = documentsPath.stringByAppendingPathComponent("filename.jpg")
imageData.writeToFile(destinationPath, atomically: false)

Then later to retrieve the image:

let loadedImage = UIImage(contentsOfFile: destinationPath)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM