我正在使用PHImageManager来挑选多个图像并像这样写。问题是它是块和序列不按顺序(当用户选择照片1,2,3时,它可能返回3,1,2)。我该如何写,以便按顺序?
PHImageManager *manager = [PHImageManager defaultManager];
[manager requestImageForAsset:asset targetSize:PHImageManagerMaximumSize
contentMode:PHImageContentModeDefault
options:self.requestOptions
resultHandler:^(UIImage *image, NSDictionary *info){
if (self.isToSelectSingleImage) {
self.mediaCollection = [NSMutableArray array];
self.thumbnailCollection = [NSMutableArray array];
}
[self addImageToMediaContainerWithImage:image.normalizedImage];
}];
答案 0 :(得分:1)
您可以创建一个下载帮助程序类,我们可以将其命名为AssetsDownloader
。
基本思想是获取一系列资产并使用递归函数下载它们。
该数组已经是有序集合,因此我们首先下载第一个资产。下载资产后,我们立即获取阵列中的下一个项目并开始下载该项目。
AssetsDownloader.h
@import Foundation;
@import Photos;
@interface AssetsDownloader : NSObject
- (void)downloadAssets:(NSArray<PHAsset *> *)assets;
@end
AssetsDownloader.m
#import "AssetsDownloader.h"
@implementation AssetsDownloader {
NSMutableArray<PHAsset *> *_assets;
}
- (void)downloadAssets:(NSArray<PHAsset *> *)assets {
_assets = [[NSMutableArray alloc] initWithArray: assets];
PHAsset *firstAsset = assets.firstObject;
[self downloadAsset:firstAsset];
}
- (void)downloadAsset:(PHAsset *)asset {
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:PHImageManagerMaximumSize
contentMode:PHImageContentModeDefault
options:nil
resultHandler:^(UIImage *image, NSDictionary *info){
// Do what you have to with the asset
// Remove first object from your assets
[_assets removeObjectAtIndex:0];
// Get the next asset from your assets
PHAsset *nextAsset = _assets.firstObject;
// Check if it exists
if(nextAsset) {
// Use the same function to dowloand the asset
[self downloadAsset:asset];
} else {
// No more assets to download
}
}];
}
@end