gpt4 book ai didi

iOS : To get selected photos in sequence with PHImageManager

转载 作者:行者123 更新时间:2023-11-29 00:11:58 25 4
gpt4 key购买 nike

我正在使用 PHImageManager 来选择多个图像并这样写。问题是它是 block 并且顺序不正确(当用户选择照片 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];
}];

最佳答案

您可以创建一个下载助手类,我们将其命名为AssetsDownloader。基本思想是获取一组 Assets 并使用递归函数下载它们。

数组已经是一个有序的集合,所以我们从下载第一个 Assets 开始。下载 Assets 后,我们将获取数组中的下一项并开始下载该项目。

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

关于iOS : To get selected photos in sequence with PHImageManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46264051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com