gpt4 book ai didi

ios - AFNetworking 2.0 完成下载多张图片

转载 作者:可可西里 更新时间:2023-11-01 05:29:06 26 4
gpt4 key购买 nike

我正在尝试找出一种使用 AFNewtorking 2.0 下载多个图像的方法。我在 SO 中阅读了很多帖子,但找不到我正在寻找的答案,希望你们能帮助我。

问题是我想知道所有下载何时完成以及是否所有图像都已下载。所以我有一个数组,其中包含图像 URL 的 Ant ,试图做这样的事情。

for(NSString *photoUrlString in self.photos){

NSURL *url = [NSURL URLWithString:photoUrlString];
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:url]];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Image error: %@", error);
}];
[requestOperation start];
}

通过将这些请求放入队列并将最大并发操作数设置为 1,我找到了一些答案。但不知道它是如何工作的。

感谢任何帮助,提前致谢!

最佳答案

for(Photo *photo in array){

//form the path where you want to save your downloaded image to
NSString *constPath = [photo imageFullPath];

//url of your photo
NSURL *url = [NSURL URLWithString:photo.serverPath];

AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:url]];
op.responseSerializer = [AFImageResponseSerializer serializer];

op.outputStream = [NSOutputStream outputStreamToFileAtPath:constPath append:NO];
op.queuePriority = NSOperationQueuePriorityLow;
[op setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead){

}];

op.completionBlock = ^{

//do whatever you want with the downloaded photo, it is stored in the path you create in constPath
};
[requestArray addObject:op];
}

NSArray *batches = [AFURLConnectionOperation batchOfRequestOperations:requestArray progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
} completionBlock:^(NSArray *operations) {

//after all operations are completed this block is called
if (successBlock)
successBlock();
}];

[[NSOperationQueue mainQueue] addOperations:batches waitUntilFinished:NO];

关于ios - AFNetworking 2.0 完成下载多张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23671720/

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