gpt4 book ai didi

ios - 异步下载 - 右模式

转载 作者:行者123 更新时间:2023-11-28 22:40:19 24 4
gpt4 key购买 nike

我需要从我的网站下载一些图片。我使用来自应用程序的图像的 url 创建了一个 XML 文件,我读取了这个文件并将所有 url 存储在一个数组中。

现在,我需要下载所有图像,我想异步地下载它,我想知道最好的方法,现在我的代码是:

    for (int i=0; i<self.arrayAggiornamenti.count; i++) {

Aggiornamento *aggiornamento = [self.arrayAggiornamenti objectAtIndex:i];
NSMutableArray *arrayPath = aggiornamento.arrayPaths;

for (int j=0; j<arrayPath.count; j++) {

NSString *urlString = [NSString stringWithFormat:@"http://www.xxx.com/%@",[arrayPath objectAtIndex:j]];

NSString *urlStringEncoded = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *urlImage = [NSURL URLWithString:urlStringEncoded];

NSURLRequest *requestImgage = [NSURLRequest requestWithURL:urlImage
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];

self.imgData = [[NSMutableData alloc] init];

self.imgConnection = [[NSURLConnection alloc] initWithRequest:requestImgage delegate:self startImmediately:YES];
}
}

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

// Start parsing XML
if (connection == self.xmlConnection) {
}
// Start download Totale
else if (connection == self.toatalConnection) {
}
// Start connessione immagini
else {
[self.imgData writeToFile:pathFile atomically:YES];
}
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

if (connection == self.xmlConnection) {
}
else if (connection == self.toatalConnection) {
}
else {
[self.imgData appendData:data];
}

}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {

if (connection == self.xmlConnection) {
}
else if (connection == self.toatalConnection) {
}
else if (connection == self.imgConnection){
}
}

使用此代码可以下载图像,但问题是如果我在 didReciveResponse 上添加 if else (connection==self.imgConnection),应用程序只下载第一张图像,为什么?这样下载多张图片正确吗?

最佳答案

可能是因为您正在循环内分配 self.imgData。请引用此链接以下载数组内的图像:What is asynchronous image downloading and how can I download too many images?

关于ios - 异步下载 - 右模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14725418/

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