iPhone - UIImagePickerController -> save the image to app folder

后端 未结 4 879
刺人心
刺人心 2020-12-12 18:47

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

  • UIImagePickerControllerSourceTypePhotoLibrary
4条回答
  •  难免孤独
    2020-12-12 19:11

    Update knuku's answer for 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)        
    }
    

    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.

提交回复
热议问题