Skip to content →

Month: October 2016

PHImageManager requestImageData with Photos iCloud Photo Library

I came across a bug the other day because of photos in the iCloud Photo Library. In my app I was showing a grid of images to be selected. If “iCloud Photo Library” was enabled then user could see the thumbnail and pick the image, but the app failed when it tried to do something with the image. This is because the image wasn’t downloaded yet.

This is what my code looked like.


let manager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.resizeMode = .exact
requestOptions.deliveryMode = .highQualityFormat;

// Request Image
manager.requestImageData(for: asset, options: requestOptions, resultHandler: { (data, str, orientation, info) -> Void in
	// Do somethign with Image Data
 })

I found the property: PHImageRequestOptions.isNetworkAccessAllowed. When enabled, the image is downloaded from iCloud. And you can set a progressHandler to inform the user.

I updated the code to look like:


let manager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.resizeMode = .exact
requestOptions.deliveryMode = .highQualityFormat;
requestOptions.isNetworkAccessAllowed = true;
requestOptions.progressHandler = { (progress, error, stop, info) in
    if(progress == 1.0){
        SVProgressHUD.dismiss()
    } else {
        SVProgressHUD.showProgress(Float(progress), status: "Downloading from iCloud")
    }
}

// Request Image
manager.requestImageData(for: asset, options: requestOptions, resultHandler: { (data, str, orientation, info) -> Void in
	// Do somethign with Image Data
 })

The requestImageData API, returns an info dictionary providing information about the status of the request. Documentation on the key is here:
PHImageManager -Image Result Info Keys

PHImageResultIsInCloudKey A Boolean value indicating whether the photo asset data is stored on the local device or must be downloaded from iCloud.

You could also check this key to see if downloading the image is required.

Comments closed