某图片添加控件:
https://github.com/XZTLLQ/LQPhotoPickerDemo
问题:
标题已说明
代码块:
NSArray *alAssetUrl =(NSMutableArray *)[HiddenRecordDataService getLocalImageSystemFilePath:_hiddenDangerInfoLocalModel.HiddenDangerInfoLocalId]; NSMutableArray *localASSetArray = [NSMutableArray arrayWithCapacity:0]; for (NSURL *url in alAssetUrl) { NSLog(@"url:%@", url); ALAssetsLibrary* library = [[ALAssetsLibrary alloc]init]; // library fetch [library assetForURL:url resultBlock:^(ALAsset *asset) { if (asset) { [localASSetArray addObject:asset]; } else { // On iOS 8.1 [library assetForUrl] Photo Streams always returns nil. Try to obtain it in an alternative way } } failureBlock:^(NSError *error) { iToast *itoast = [iToast makeText:@"获取图片失败"]; [itoast show]; }]; }
问题是由于标红代码导致,这里的ALAssetsLibrary对象需要用单例
代码如下:
NSArray *alAssetUrl =(NSMutableArray *)[HiddenRecordDataService getLocalImageSystemFilePath:_hiddenDangerInfoLocalModel.HiddenDangerInfoLocalId]; NSMutableArray *localASSetArray = [NSMutableArray arrayWithCapacity:0]; for (NSURL *url in alAssetUrl) { NSLog(@"url:%@", url); //ALAssetsLibrary* library = [[ALAssetsLibrary alloc]init]; ALAssetsLibrary* library = [HiddenRecordViewController defaultAssetsLibrary]; // library fetch [library assetForURL:url resultBlock:^(ALAsset *asset) { if (asset) { [localASSetArray addObject:asset]; } else { // On iOS 8.1 [library assetForUrl] Photo Streams always returns nil. Try to obtain it in an alternative way } } failureBlock:^(NSError *error) { iToast *itoast = [iToast makeText:@"获取图片失败"]; [itoast show]; }]; }
defaultAssetsLibrary代码:
+ (ALAssetsLibrary *)defaultAssetsLibrary { static dispatch_once_t pred = 0; static ALAssetsLibrary *library = nil; dispatch_once(&pred, ^{ library = [[ALAssetsLibrary alloc] init]; }); return library; }