Thyraz Thyraz - 1 year ago 110
iOS Question

How to identify if a PHAsset is not completely downloaded from iCloud (so I need to request again with options.networkAccessAllowed)

The docs say:

A Boolean value indicating whether the photo asset data is stored on the local device or must be downloaded from iCloud. (NSNumber)
If YES, no image was provided, because the asset data must be downloaded from iCloud. To download the data, submit another request, and specify YES for the networkAccessAllowed option.

But this key is always YES when a asset is stored in the iCloud Photo Library, even when it is already completely downloaded to the device (downloaded it in my app, also opened it in the Photos app).

If an image is not available I want to give the user a possibility to download it (but don't do it automatically, at least not when no Wifi is around).

So how do I find out if the image needs to be downloaded?

Even more curious: When my result block of

is called for an image that needs to be downloaded, I get a last call with requestedImage == nil, after a smaller and degraded version was delivered.

In this case degraded is NO, even that I got no image and the image still has to be downloaded from iCloud as only a small thumbnail from the Photos app is locally available so far.

I tested this on iPhones and iPads with different iOS 8 versions (8.1.x, 8.2 beta, 8.3 beta) the behavior is always the same.

Once I opened the image in the Photos app, the last call of the result handler has the full size image, but PHImageResultIsInCloudKey will still be YES.

Here's some code how I request the images:

PHImageRequestOptions *options = [[PHImageRequestOptions alloc]init];
options.deliveryMode = PHImageRequestOptionsDeliveryModeOpportunistic;
options.networkAccessAllowed = NO;

[self.imageManager requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeAspectFill options:options resultHandler:^(UIImage *requestedImage, NSDictionary *info) {
// Checking for requestedImage and the info keys here
// When a full sized image was loaded, the result of PHImageResultIsInCloudKey is still YES
// When a full sized image couldn't be loaded cause it's in the cloud, isDegraded is NO and PHImageResultIsInCloudKey is YES (as always) and requestedImage is nil

Answer Source

I can confirm that PHImageResultIsInCloudKey is not reliable. For images stored in iCloud it returns 1, even though the original image has been downloaded to the device. This behavior is in contrast to the documentation and I would suggest to report a bug at PhotoKit in my opinion is still a very immature framework - it contains lots of issues and also some weird conceptional decisions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download