18

When I try to access pictures from a photo stream, sometimes the [asset defaultRepresentation] method returns nil.

According to the documentation this can occur if the asset is not (yet) available locally.

This method returns nil for assets from a shared photo stream that are not yet available locally. If the asset becomes available in the future, an ALAssetsLibraryChangedNotification notification is posted.

Once I view the photo in the Photos.app it becomes available for my app too, but obviously I'd like to have my app trigger the "download locally" event.

I looked at the MyImagePicker sample code from Apple, but it exhibits the same behaviour (only a thumbnail is available)

How can I make the asset available locally?

Edit: Since Apple introduced the Photos.framework in iOS8, and using ALAssets became deprecated in iOS9, this will probably never be fixed.

Pieter
  • 17,435
  • 8
  • 50
  • 89
  • 1
    Running into this problem as well, seeing it more often running on iOS7. Other apps seem to have a way to load these images, so there must be a way, but reading the ALAsset-related docs hasn't revealed anything... – Laura Aug 24 '13 at 14:42
  • @Laura I see this on iOS7 too, I can't say I've seen other apps handle the same photos correctly though. (if such an image doesn't load in my app, I switch to instagram, and it crashed when trying to access the same photo). Once I opened the photo in Apple's Photo app, it works everywhere correctly. – Pieter Aug 24 '13 at 17:19
  • Pic Collage is the app I ran which seemed to work (at least sometimes - other times it just crashes). When I select a thumbnail from the photo picker, I can see it add the photo first as a blurry, square version of the thumbnail, and then a second or so later, it replaces the thumbnail with the full size image. So, it seems to go off and load it, but not sure now. I tried create a new album, adding the asset to that album, and then attempting to enumerate the assets in that group, but no dice either. – Laura Aug 26 '13 at 02:56
  • I'm running into this problem, too. In the documentation I haven't found anything relevant, unfortunately. – Dev Aug 28 '13 at 13:08
  • Anyone found the solution yet? – Mark May 22 '14 at 14:48
  • @Mark not as far as I know – Pieter May 22 '14 at 14:49

3 Answers3

4

I fall into same issue. I couldn't find a way to actually get the images. All you can do is not display images that have "defaultRepresentation" nil or drop support for Shared photos at all by calling [ALAssetsLibrary disableSharedPhotoStreamsSupport];

Hope that helps

Inshiqaq
  • 41
  • 2
2

I found the answer! It is not documented and therefor use the following answer with caution. (With caution I mean, Apple doesn't allow to use private API calls).

On the asset there is a method named - (void)requestDefaultRepresentation; you can use this method to trigger the Asset Library to download the shared photo. As stated in the document the ALAssetsLibraryChangedNotification will be fired to let you know the image became available.

No idea, why Apple didn't document it. Good luck have fun =)

You can avoid a warning with the following header:

ALAsset+RequestDefaultRepresentation.h

#import <AssetsLibrary/AssetsLibrary.h>

@interface ALAsset (RequestDefaultRepresentation)
- (void)requestDefaultRepresentation;
@end
Mark
  • 16,906
  • 20
  • 84
  • 117
  • Good to know, but using a private API is obviously not an acceptable solution for apps on the Appstore. – Pieter May 23 '14 at 08:08
  • 1
    I understand, at least it gives clarity that it exists and the way it could be done. There is no other way. – Mark May 23 '14 at 08:24
1

Since PhotoKit framework has been introduced by Apple in iOS8, and all ALAsset stuff became deprecated in iOS9, it's safe to say this will never get fixed.

This can, however, be done properly with the new PhotoKit API's.

You have to set the networkAccessAllowed = YES on the PHImageRequestOptions when asking for an image, and it will download it from iCloud if needed.

 PHAsset* photoAsset = ...
 CGSize fullImageSize = ...
 PHImageRequestOptions* options = [PHImageRequestOptions new];
 options.networkAccessAllowed = YES;
 [[PHImageManager defaultManager] requestImageForAsset:photoAsset
      targetSize:fullImageSize  contentMode:PHImageContentModeAspectFill
      options:options  resultHandler:^(UIImage *result, NSDictionary *info) {  /* use result image */  }];
Pieter
  • 17,435
  • 8
  • 50
  • 89