iosswiftuiimageuiimagepickercontrollerios11.2

UIImagePickerControllerOriginalImage is not working in ios 11.2.1


In what cases will I be sad? if I have set allowEditing as false.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        // I am happy :)
    } else {
        // I am sad :(
    }
    dismiss(animated: true, completion: nil)
}

(I got a crash in iOS 11.2.1 iPhone SE(as per Crashlytics), so confused if there are legit conditions where this can fail or it is just an iOS bug.)


Solution

  • I ended up using this:

    import Photos
    
    extension UIImage {
        static func from(info: [String : Any]) -> UIImage? {
            if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
                return image
            }
    
            var imageToBeReturned: UIImage?
            if let url = info[UIImagePickerControllerReferenceURL] as? URL,
                let asset = PHAsset.fetchAssets(withALAssetURLs: [url], options: nil).firstObject {
                let manager = PHImageManager.default()
                let option = PHImageRequestOptions()
                option.isSynchronous = true
                manager.requestImage(for: asset, targetSize: CGSize(width: 1000, height: 1000), contentMode: .aspectFit, options: option, resultHandler: {(image: UIImage?, info: [AnyHashable : Any]?) in
                    imageToBeReturned = image
                })
            }
            return imageToBeReturned
        }
    }
    

    In this way-

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let selectedImage = UIImage.from(info: info) {
            // I am happy :)
        } else {
            // I am sad :(
        }
        dismiss(animated: true, completion: nil)
    }
    

    This is working for me, please do suggest any improvements :)