对于 URL 列表,我需要使用 ALAssetsLibrary:assetForURL 加载照片,这在一个方法中。众所周知,由于此方法异步工作,但它不会遍历传递的 URL 列表。我发现了这个片段(应该可以):
- (void)loadImages:(NSArray *)imageUrls loadedImages:(NSArray *)loadedImages callback: (void(^)(NSArray *))callback
{
if (imageUrls == nil || [imageUrls count] == 0) {
callback(loadedImages);
}
else {
NSURL *head = [imageUrls head];
__unsafe_unretained id unretained_self = self;
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library assetForURL:head resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *assetRepresentation = asset.defaultRepresentation;
UIImage *image = [UIImage imageWithCGImage:assetRepresentation.fullResolutionImage scale:assetRepresentation.scale orientation:(UIImageOrientation)assetRepresentation.orientation];
[unretained_self loadImages:[imageUrls tail] loadedImages:[loadedImages arrayByAddingObject:image] callback:callback];
} failureBlock:^(NSError *error) {
[unretained_self loadImages:[imageUrls tail] loadedImages:loadedImages callback:callback];
}];
}
}
如何在表单中编写方法定义(首先是回调)
void loadImages(NSArray *imageUrls, NSArray *loadedImages, ...) ?
如何从另一种方法(再次主要是回调部分)调用此方法?回调可以在调用方法中还是在此所需的第三种方法中?这个方法需要怎么写?我在这里找到了片段:http: //www.calebmadrigal.com/functional-programming-deal-asynchronicity-objective-c/