My Photo Stream (我的照片流)ALAsset return nil

做了一个图片多选的功能,因为要支持iOS6,所以使用了AssetsLibrary遍历读取照片,但是选取我的照片流相册中的照片时,通过NSUrl,即图片的地址读取该照片ALAsset 为nil.
看了一下百度云将我的照片流过滤掉了,360云盘也出现了选取图片为nil 的情况.

google了一下, 以下为解决方法,如果返回为nil的话,需要再重新遍历一次group,将其取出:

  • (void)loadItem:(NSURL *)url withSuccessBlock:(ALAssetsLibraryAssetForURLResultBlock)resultBlock andFailureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock {
    ALAssetsLibrary *assetslibrary = [[ALAssetsLibrary alloc] init];

    [assetslibrary assetForURL:url
    resultBlock:^(ALAsset *asset)
    {
    if (asset){
    //////////////////////////////////////////////////////
    // SUCCESS POINT #1 - asset is what we are looking for
    //////////////////////////////////////////////////////
    resultBlock(asset);
    }
    else {
    // On iOS 8.1 [library assetForUrl] Photo Streams always returns nil. Try to obtain it in an alternative way

           [assetslibrary enumerateGroupsWithTypes:ALAssetsGroupPhotoStream
                                        usingBlock:^(ALAssetsGroup *group, BOOL *stop)
            {
                [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                    if([result.defaultRepresentation.url isEqual:url])
                    {
                        ///////////////////////////////////////////////////////
                        // SUCCESS POINT #2 - result is what we are looking for
                        ///////////////////////////////////////////////////////
                        resultBlock(result);
                        *stop = YES;
                    }
                }];
            }
               failureBlock:^(NSError *error)
            {
                NSLog(@"Error: Cannot load asset from photo stream - %@", [error localizedDescription]);
                failureBlock(error);
                
            }];
       }
    

    }
    failureBlock:^(NSError *error)
    {
    NSLog(@"Error: Cannot load asset - %@", [error localizedDescription]);
    failureBlock(error);
    }
    ];
    }

你可能感兴趣的:(My Photo Stream (我的照片流)ALAsset return nil)