Ios 如何使用AFNetworking从一系列URL下载和存储图像?
我使用的是Ios 如何使用AFNetworking从一系列URL下载和存储图像?,ios,iphone,uiimageview,uiimage,afnetworking,Ios,Iphone,Uiimageview,Uiimage,Afnetworking,我使用的是“UIImageView+AFNetworking.h”,我的目标是获取一个名为imageURLArray的URL数组,下载所有图片,将它们放在带有页面控件的滚动视图中 为此,我在下面的循环中一次下载一张图片,并使用先前分配/初始化的可变数组downloadImageArray,在其中存储下载的图片: for (int i = 0; i < [imageURLArray count]; i++){ NSURL *testURL = [imageURLArray o
“UIImageView+AFNetworking.h”
,我的目标是获取一个名为imageURLArray
的URL数组,下载所有图片,将它们放在带有页面控件的滚动视图中
为此,我在下面的循环中一次下载一张图片,并使用先前分配/初始化的可变数组downloadImageArray
,在其中存储下载的图片:
for (int i = 0; i < [imageURLArray count]; i++){
NSURL *testURL = [imageURLArray objectAtIndex:i];
NSURLRequest *testURLRequest = [[NSURLRequest alloc] initWithURL:testURL];
[placeHolderImageView setImageWithURLRequest:testURLRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
NSLog(@"success");
NSLog(@"The image is %@", image);
[downloadedImageArray addObject:image]; //! the exception is thrown here - it says the object is NULL
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"failed to download");
}];
}
NSLog(@"Count of images is %i", [downloadedImageArray count]) //this always returns "0"
for(int i=0;i<[imageURLArray count];i++){
NSURL*testURL=[imageURLArray对象索引:i];
NSURLRequest*testURLRequest=[[NSURLRequest alloc]initWithURL:testURL];
[占位符图像查看setImageWithURLRequest:testURLRequest占位符图像:无成功:^(NSURLRequest*请求,NSHTTPURLResponse*响应,UIImage*图像){
NSLog(“成功”);
NSLog(@“图像为%@”,图像);
[downloadedImageArray addObject:image];/!在此处引发异常-它表示对象为空
}失败:^(NSURLRequest*请求,NSHTTPURLResponse*响应,NSError*错误){
NSLog(@“下载失败”);
}];
}
NSLog(@“图像计数为%i,[downloadeImageArray计数])//这始终返回“0”
作为我将对象添加到downloadeImageArray
的行中的注释,我得到一个异常,即添加到可变数组的对象为NULL。我怀疑这是因为在下一个URL设置到位之前,图像还没有完成下载,并且被要求再次下载。同样,在循环之后,当我得到一个计数downloadeImageArray
时,计数返回0
我的问题是:
imageURLArray
的URL集不超过4个答1:我们可以使用nsrunlop使等待进程完成变为尝试以下操作
for (int i = 0; i < [imageURLArray count]; i++)
{
NSURL *testURL = [imageURLArray objectAtIndex:i];
NSURLRequest *testURLRequest = [[NSURLRequest alloc] initWithURL:testURL];
[placeHolderImageView setImageWithURLRequest:testURLRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
NSLog(@"success");
NSLog(@"The image is %@", image);
while (!image)
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
[downloadedImageArray addObject:image]; //! the exception is thrown here - it says the object is NULL
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"failed to download");
}];
}
NSLog(@"Count of images is %i", [downloadedImageArray count]);
for(int i=0;i<[imageURLArray count];i++)
{
NSURL*testURL=[imageURLArray对象索引:i];
NSURLRequest*testURLRequest=[[NSURLRequest alloc]initWithURL:testURL];
[占位符图像查看setImageWithURLRequest:testURLRequest占位符图像:无成功:^(NSURLRequest*请求,NSHTTPURLResponse*响应,UIImage*图像){
NSLog(“成功”);
NSLog(@“图像为%@”,图像);
而(!图像)
[[NSRunLoop currentRunLoop]运行模式:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
[downloadedImageArray addObject:image];/!在此处引发异常-它表示对象为空
}失败:^(NSURLRequest*请求,NSHTTPURLResponse*响应,NSError*错误){
NSLog(@“下载失败”);
}];
}
NSLog(@“图像计数为%i,[下载的图像数组计数]);
现在,在未下载阵列之前,不会将图像添加到阵列中
答2:按照此流程显示图像
a。下载图像
b。添加到数组
c。添加到滚动条
请执行此操作,直到未下载所有映像。答案。1我们可以使用nsrunlop等待进程完成。请尝试此操作
for (int i = 0; i < [imageURLArray count]; i++)
{
NSURL *testURL = [imageURLArray objectAtIndex:i];
NSURLRequest *testURLRequest = [[NSURLRequest alloc] initWithURL:testURL];
[placeHolderImageView setImageWithURLRequest:testURLRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
NSLog(@"success");
NSLog(@"The image is %@", image);
while (!image)
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
[downloadedImageArray addObject:image]; //! the exception is thrown here - it says the object is NULL
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"failed to download");
}];
}
NSLog(@"Count of images is %i", [downloadedImageArray count]);
for(int i=0;i<[imageURLArray count];i++)
{
NSURL*testURL=[imageURLArray对象索引:i];
NSURLRequest*testURLRequest=[[NSURLRequest alloc]initWithURL:testURL];
[占位符图像查看setImageWithURLRequest:testURLRequest占位符图像:无成功:^(NSURLRequest*请求,NSHTTPURLResponse*响应,UIImage*图像){
NSLog(“成功”);
NSLog(@“图像为%@”,图像);
而(!图像)
[[NSRunLoop currentRunLoop]运行模式:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
[downloadedImageArray addObject:image];/!在此处引发异常-它表示对象为空
}失败:^(NSURLRequest*请求,NSHTTPURLResponse*响应,NSError*错误){
NSLog(@“下载失败”);
}];
}
NSLog(@“图像计数为%i,[下载的图像数组计数]);
现在,在未下载阵列之前,不会将图像添加到阵列中
答2:按照此流程显示图像
a。下载图像
b。添加到数组
c。添加到滚动条
执行此操作直到未下载所有图像。如果您使用的是最新版本的AFNetworking,要下载图像,可以执行以下操作:
NSURLRequest* request = [...]
AFHTTPRequestOperation* op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFImageResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation* operation, id responseObject){...} failure:^(AFHTTPRequestOperation* operation, NSError* error) {...}];
[op start];
因此,如果您想循环浏览一系列URL并从每个URL下载一个图像,您应该这样做(还有一句警告:我还没有测试过这个):
NSMutableArray*images=[NSMutableArray阵列容量:imageURLArray.count];
对于(int i=0;i