iosswiftphasset

PHAsset is returning a nil value for the UIImage when using PHImageManager


Any idea why this would return nil for 'thisImage' after adding in the requestImageOption settings? If options is set to nil thisAsset returns a value but it's a low res image, definitely not original high res. I'm trying to return the original image from the PHAsset (thisAsset)

    if let thisAsset:PHAsset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset {
          arrImageIdentifiers.append(thisAsset.localIdentifier)

        let manager = PHImageManager.default()
        let requestImageOption = PHImageRequestOptions()
        requestImageOption.deliveryMode = PHImageRequestOptionsDeliveryMode.highQualityFormat

        manager.requestImage(for: thisAsset, targetSize: PHImageManagerMaximumSize, contentMode: PHImageContentMode.default, options: requestImageOption, resultHandler: {(thisImage, _) in

            Object.tempImage = thisImage

            print("Picture metadata: \(thisAsset)")
            let creationDate = thisAsset.creationDate

            PhotoMeta.createDate = creationDate

            })
            //Get a reference to the camera view controller and call the savePhoto method
            let cameraVC = self.selectedViewController as? CameraViewController

            if let cameraVC = cameraVC {
                cameraVC.savePhoto(image: Object.tempImage!)
            }
    }

Solution

  • Set requestOptions's isSynchronus to true to resolve your problem:

        let requestImageOption = PHImageRequestOptions()
        requestImageOption.deliveryMode = PHImageRequestOptionsDeliveryMode.highQualityFormat
        requestImageOption.isSynchronous = true
    
        manager.requestImage(for: thisAsset, targetSize: PHImageManagerMaximumSize, contentMode: PHImageContentMode.default, options: requestImageOption, resultHandler: {(thisImage, _) in
    
            //....
        }