我正在尝试获取photos.app中的所有图像,并将它们显示在UICollectionView中.我有这个代码检索图像:
ALAssetsLibrary *al = [ViewController defaultAssetsLibrary]; ALAssetsGroupEnumerationResultsBlock groupEnumerAtion = ^(ALAsset *result,NSUInteger index,BOOL *stop){ if (result!=NULL) { if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) { [imagesGotFromUserLibrary addObject:result]; } } }; ALAssetsLibraryGroupsEnumerationResultsBlock libraryGroupsEnumeration = ^(ALAssetsGroup* group,BOOL* stop){ [group setAssetsFilter:[ALAssetsFilter allPhotos]]; if (group!=nil) { [group enumerateAssetsUsingBlock:groupEnumerAtion]; } else { dispatch_async(dispatch_get_global_queue(0,0),^{ GalleryCollectionViewController *otherController = [[GalleryCollectionViewController alloc] init]; [otherController receiveImagesWithMutableArray:imagesGotFromUserLibrary]; }); } }; al = [[ALAssetsLibrary alloc] init]; [al enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:libraryGroupsEnumeration failureBlock:^(NSError *error){ NSLog(@"ERROR: %@",error); }];
这是在viewDidLoad然后:
+ (ALAssetsLibrary *)defaultAssetsLibrary { static dispatch_once_t pred = 0; static ALAssetsLibrary *library = nil; dispatch_once(&pred,^{ library = [[ALAssetsLibrary alloc] init]; }); return library; }
这段代码是将数组发送到另一个控制器,将控制器设置为UICollectionView.问题是,我收到错误“无效尝试访问其拥有的ALAssetsLibrary的生命周期”,如果我尝试使用NSLog我的数组,结果就像“ALAsset – Type:Unknown”,URL:(null)“.
我抬头看了互联网,找到了一个解决方案.我应该添加这行代码,但它不起作用.代码是:
+ (ALAssetsLibrary *)defaultAssetsLibrary { static dispatch_once_t pred = 0; static ALAssetsLibrary *library = nil; dispatch_once(&pred,^{ library = [[ALAssetsLibrary alloc] init]; }); return library; }