ALAssetsLibrary assetForURL:对于“我的照片流”中的照片,总是返回nil在iOS 8.1中 [英] ALAssetsLibrary assetForURL: always returning nil for photos in "My Photo Stream" in iOS 8.1

查看:240
本文介绍了ALAssetsLibrary assetForURL:对于“我的照片流”中的照片,总是返回nil在iOS 8.1中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码在iOS 7中运行良好,但在iOS 8.1中,位于我的照片流专辑中的所有资源都不在结果块中。 (不调用failureBlock。)常规专辑和共享专辑工作得很好。

This code worked fine in iOS 7 but in iOS 8.1 all assets located in the "My Photo Stream" album are nil from within the result block. (The failureBlock is not called.) Regular albums and shared albums work just fine.

我尝试了接受的答案:尝试从assetForURL内部分配__block ALAsset时出错:resultBlock:

I tried the accepted answer from: Error trying to assigning __block ALAsset from inside assetForURL:resultBlock:

也就是说,我持有对ALAssetsLibrary对象的引用,侦听 ALAssetsLibraryChangedNotification 事件(不会发生btw,但是很好。)我确保我的应用程序有权访问照片,我正在使用Wi-Fi,我在桌面视图中看到照片的缩略图。只是当我尝试使用 assetForURL:加载它们时,它们总是为零。

That is, I'm holding a reference to an ALAssetsLibrary object, listening for the ALAssetsLibraryChangedNotification event (which doesn't happen btw, but oh well.) I made sure my app has permission to access photos, I'm on wi-fi, I see the photos' thumbnails just fine in my tableView. It's just when I try to load them with assetForURL: they're always nil.

// example URL: assets-library://asset/asset.JPG?id=1ECB69B9-DC7A-45A7-B135-F43317D3412C&ext=JPG
[self.library assetForURL:[NSURL URLWithString:url] resultBlock:^(ALAsset *asset) {
    NSLog(@"Asset: %@", asset); // nil :(
} failureBlock:^(NSError *error) {
    NSLog(@"Failure, wahhh!");
}];

还有其他人看到这个问题吗?

Is anyone else seeing this issue?

推荐答案

<我遇到了同样的问题。此时切换到Photos框架对我来说不是一个选项,但幸运的是我找到了一个解决方法。你可能会发现它很难看,我怀疑它可能会在Photo Stream包含很多内容时运行缓慢照片,但总比没有好。

I had the same problem. Switching to Photos framework is not an option for me at this moment, but fortunately I have found a workaround. You may find it a big ugly and I suspect it may work slow when Photo Stream contains a lot of photos, but it is better than nothing.

想法是枚举Photo Stream资产组中的所有项目,并将必要的URL与每个项目的URL进行比较。幸运的是,它仍然有效。

The idea is to enumerate all items in the Photo Stream asset group and compare the necessary URL with the URL of each item. Fortunately, it still works.

我有一个像这样的方法(库是同一个类的ALAssetsLibrary属性,你可能需要在这个代码中初始化它):

I have a method like this (library is ALAssetsLibrary property of the same class, you may need to initialise it inside this code):

- (void)loadItem:(NSURL *)url withSuccessBlock:(void (^)(void))successBlock andFailureBlock:(void (^)(void))failureBlock {

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

                [library 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
                             ///////////////////////////////////////////////////////
                             successBlock();
                             *stop = YES;
                         }
                     }];
                 }

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

                 }];
            }

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

希望这有帮助。

这篇关于ALAssetsLibrary assetForURL:对于“我的照片流”中的照片,总是返回nil在iOS 8.1中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆