PHAsset returned from fetchAssetsWithALAssetURLs: is always nil when choosing a photo from the "My Photo Stream" album using UIImagePickerController
Asked Answered
B

2

9

I am using a UIImagePickerController to let the user choose a photo or video to share in the app. When the user chooses a media item in their Library, I execute this code in one of the UIImagePickerController's delegate methods:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

PHAsset *asset;
if ([info[@"UIImagePickerControllerMediaType"] isEqualToString:@"public.movie"]) {
    // Video
    asset = [[PHAsset fetchAssetsWithALAssetURLs:@[info[@"UIImagePickerControllerReferenceURL"]] options:nil] lastObject];

} else if ([info[@"UIImagePickerControllerMediaType"] isEqualToString:@"public.image"]) {
    // Photo
    PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[info[@"UIImagePickerControllerReferenceURL"]] options:nil];
    asset = [[PHAsset fetchAssetsWithALAssetURLs:@[info[@"UIImagePickerControllerReferenceURL"]] options:nil] lastObject];  
   }
}

Both if statements work fine for both photo and video, except for when you select an item from the album titled "My Photo Stream".

When you select an item from "My Photo Stream", the returned PHAsset is always nil.

I found the following question that appears to have an answer with a working solution: ALAssetsLibrary assetForURL: always returning nil for photos in "My Photo Stream" in iOS 8.1

But the above link uses the AssetsLibrary framework which is no longer recommended by Apple:

"In iOS 8.0 and later, use the Photos framework instead of the Assets Library framework. The Photos framework provides more features and better performance for working with a user’s photo library. See Photos Framework Reference."

I need to be able to return PHAsset objects for the media items in the "My Photo Stream" album. Right now the reference URL that's returned by the UIImagePickerController in the info dictionary is a valid URL that logs in the console, but when using this URL, a valid PHAsset object is never returned.

Here is an example of a reference URL that is returned in the didFinishPickingMediaWithInfo: delegate method's info dictionary:

assets-library://asset/asset.JPG?id=DCF5C6E5-B4F4-4E61-9C4B-CC63E104BF2B&ext=JPG
Biebel answered 16/12, 2014 at 3:7 Comment(0)
F
1

It's a bug and seems to be fixed in the latest iOS 8.2x betas.

Foch answered 13/2, 2015 at 12:27 Comment(7)
I have just update to IOS 8.2, unfortunately it still doesn't work.Enforce
still seems to be a problem even with 8.4.1.Extreme
iOS 8.1 works but iOS 8.1.2 seems to return nil all the timeFinnigan
Doesn't work on iOS 9.2.1, waiting to see 9.3 which was released yesterday.Finnigan
I see this problem on iOS 9.3Dyspeptic
same problem on 9.3 here :/Contorted
It's crazy man. Same problem on 10.3.3. How on earth could a bug persist since more than 2 years??Nonviolence
N
1

It's unbelievable but as of iOS 10.3.3 the bug persists and it looks like it was fixed by only iOS 11...

In order to cover iOS 10 and below I'm using fetchAssets of PHAsset with info dictionary returned from didFinishPickingMediaWithInfo delegate method:

PHAsset.fetchAssets(withALAssetURLs: [info["UIImagePickerControllerReferenceURL"] as! URL], options: nil)

Remember that this returns an array of results.

Nonviolence answered 23/10, 2017 at 14:47 Comment(1)
Isn't your code the same as in the original question?Suspect

© 2022 - 2024 — McMap. All rights reserved.