iosuiimagepickercontrollerios13phlivephoto

UIImagePickerController doesn't return a live photo in iOS 13


My code has stopped working in iOS 13. In my UIImagePickerController delegate I look to see if the media type returned (the type of thing the user chose) is a live photo. If it is, I use the .livePhoto key to get the PHLivePhoto. This used to work. But in iOS 13 this key's value is always nil. How can I get the live photo that the user chose?


Solution

  • I regard this change in behavior as a bug. However, if you have permission to access the user's photo library, you can use the .phAsset value, a PHAsset, to get the corresponding PHLivePhoto directly from the photo library yourself.

            let asset = info[.phAsset] as? PHAsset
            if let style = asset?.playbackStyle {
                switch style {
                case .livePhoto:
                    // hmm, I'm getting .livePhoto but live _is_ nil!
                    if live != nil {
                        self.showLivePhoto(live!)
                    } else {
                        print("live is nil!")
                        // well then I'll fetch it myself, said the little red hen
                        if let asset = asset {
                            PHImageManager.default().requestLivePhoto(
                                for: asset, targetSize: self.redView.bounds.size, 
                                contentMode: .aspectFit, options: nil) { 
                                photo, info in
                                if let photo = photo {
                                    self.showLivePhoto(photo)
                                }
                            }
                        }
                    }
                // ... other cases ...
                @unknown default: fatalError()
                }
            }