Reputation: 6143
I am using PHImageManager to pick multiple images and wrote like this. Problem is that it is block and sequence are not in order (When user select photo 1,2,3, it may return 3,1,2). How shall I write so that it will be in order?
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];
}];
Upvotes: 0
Views: 159
Reputation: 6151
You can create a download helper class, lets call it AssetsDownloader
.
The basic idea, is to get an array of assets and download them with a recursive function.
The array is already an ordered collection, so we start with downloading the first asset. As soon as the asset is downloaded, we take the next item in the array and start downloading that one.
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
Upvotes: 1