我正在编写一个多线程应用程序,该应用程序需要在后台从ALAssetsLibrary集中上传照片。因此,我有一个NSOperation子类,该子类可通过资产的URL查找适当的ALAset,并将图像添加到上传队列中。
在当前ALAsset的上传队列中,我需要从图像中获取元数据,但是我遇到了一个问题:-metadata和-fullResolutionImage方法在对ALAsset的ALAssetRepresentation进行调用时都不会返回。他们只是无限期地挂在那里。我尝试在LLDB中打印每个方法的值,但它使调试器挂起,最终我杀死了信号9样式的Xcode。这些方法在后台队列中被调用。
我正在iPad 2上进行测试。这是在-assetForURL:resultBlock:failureBlock成功块中找到ALAsset时将其添加到上传队列的方法:
- (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];
}
}];
});
}
}
最佳答案
我有一个类似的问题,我想把头发扯掉。
原来,我以为我为ALAssetsLibrary设置了一个单例,但我的代码未正确调用它,并且某些ALAssets返回空的“fullResolutionImage”
在我所有的NSLogs中,我一定错过了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/
希望对您有所帮助