ALAssetRepresentation fullResolutionImage永远不会返回

时间:2012-11-15 18:05:20

标签: objective-c ios alassetslibrary

我正在编写一个多线程应用程序,需要在后台集中上传ALAssetsLibrary中的照片。所以我有一个NSOperation子类,它通过资产的URL找到适当的ALAsset,并将图像添加到上传队列。

在当前ALAsset的上传队列中,我需要从图像中获取元数据,但是我遇到了一个问题:-metadata和-fullResolutionImage方法在ALAssetRepresentation上调用它们时都不会返回ALAsset。他们只是无限期地挂在那里。我尝试在LLDB中打印这些方法中的每一个的值,但它挂起了调试器,我最终杀死了Xcode,信号9样式。这些方法是在后台队列上调用的。

我在iPad 2上测试这些。这是在-assetForURL的成功块中找到ALAsset时添加到上传队列的方法:resultBlock:failureBlock:

- (void)addMediaToUploadQueue:(ALAsset *)media {
    @autoreleasepool {
        ALAssetRepresentation *defaultRepresentation = [media defaultRepresentation];
        CGImageRef fullResolutionImage = [defaultRepresentation fullResolutionImage];

        // Return if the user is trying to upload an image which has already been uploaded
        CGFloat scale = [defaultRepresentation scale];
        UIImageOrientation orientation = [defaultRepresentation orientation];

        UIImage *i = [UIImage imageWithCGImage:fullResolutionImage scale:scale orientation:orientation];
        if (![self isImageUnique:i]) return;

        NSDictionary *imageDictionary = [self dictionaryForAsset:media withImage:i];

        dispatch_async(self.background_queue, ^{
            NSManagedObjectContext *ctx = [APPDELEGATE createManagedObjectContextForThread];
            [ctx setUndoManager:nil];

            [ctx performBlock:^{
                ImageEntity *newImage = [NSEntityDescription insertNewObjectForEntityForName:@"ImageEntity"
                                                                    inManagedObjectContext:ctx];

                [newImage updateWithDictionary:imageDictionary
                         inManagedObjectContext:ctx];

                [ctx save:nil];

                [APPDELEGATE saveContext];

                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.fetchedResultsController performFetch:nil];
                });

                    if (!currentlyUploading) {
                        currentlyUploading = YES;
                        [self uploadImage:newImage];
                    }
            }];
        });
    }
}

1 个答案:

答案 0 :(得分:6)

我遇到了类似的问题,我正试图弄清楚我的头发。

当我以为我为ALAssetsLibrary设置一个单例时,我的代码没有正确调用它并且一些ALAssets返回一个空的'fullResolutionImage'

在我的所有NSLog中,我一定错过了Xcode最重要的信息:

“无法尝试在其拥有的ALAssetsLibrary的生命周期内访问ALAssetPrivate”

点击此链接

http://www.daveoncode.com/2011/10/15/solve-xcode-error-invalid-attempt-to-access-alassetprivate-past-the-lifetime-of-its-owning-alassetslibrary/

我希望有帮助