我正在使用ALAssetsLibrary来保存从在线服务器下载的图像。每个图像都是具有图像细节的imageObject。它将保存到相册(保存和检索网址,并将网址转换为图像的UUID)。将UUID添加到imageObject后,imageObject将添加到REALM数据库。
问题是没有执行保存到相册的代码。它被忽略了。我希望我的问题很清楚,我该怎么做才能使我的代码(块)可执行?
[self writeImagesSuccessively:[[NSMutableArray alloc] initWithArray:tempOSArray] completion:^(id result) {
NSString *res = (NSString *)result;
if ([res isEqualToString:@"finished"]) {
//Add to realm
} else {
NSLog(@"Unable to finish writing images");
}
}];
上面的代码将调用此方法,该方法将调用addImageToAssetLibrary将照片保存到相册并为每个单独的图像返回唯一的UUID。
- (void) writeImagesSuccessively:(NSMutableArray*)imgArr completion:(void(^)(id result))completionHandler {
if ([imgArr count] == 0) {
if (completionHandler) {
// Signal completion of writing all images in imgArr
completionHandler(@"finished");
}
return;
}
ImageObject* imgObj = [imgArr firstObject];
[imgArr removeObjectAtIndex:0];
__block __typeof__(self) _self = self;
[self addImageToAssetLibrary:imgObj completion:^(BOOL success, NSString *response) {
if (success) {
imgObj.imageName = response;
NSLog(@"***SAVED IMAGE: %@, %@, %lu", imgObj.imageName, response, (unsigned long)[imgArr count]);
} else {
NSLog(@"Could not save %@", imgObj.imageName);
}
[_self writeImagesSuccessively:imgArr completion:completionHandler];
}];
}
此方法将imgObj数组中的ImageObject添加到相册并返回UUID。但是,// 1) Create Album
[self.library addAssetsGroupAlbumWithName:albumName resultBlock:^(ALAssetsGroup *group) {
NSLog(@"added album:%@", albumName);
}
未被执行。
- (void) addImageToAssetLibrary:(ImageObject *)imgObj completion:(void(^)
(BOOL success, NSString* response))completion
{
NSData *imgData = UIImageJPEGRepresentation(imgObj.image, 0.5);
NSString *albumName = @"AlgoAccess Photo Album";
// What is __block?
// Distinct storage type, where modifications to the var done inside the block are also visible outside of it
__block __typeof__(self) _self = self;
__block ALAssetsGroup* groupToAddTo;
__block ALAssetsLibrary* library;
// 1) Create Album **NOT EXECUTED
[self.library addAssetsGroupAlbumWithName:albumName resultBlock:^(ALAssetsGroup *group) {
NSLog(@"added album:%@", albumName);
}
} //**NEXT LINE OF EXECUTION IS HERE
编辑:1可疑,我的块没有被调用,而其他一些块被调用。 Any1可以建议我如何调试这些原因?