2014-02-17 96 views
2

我认为我在问一个愚蠢的问题,但只是好奇。 有什么方法可以知道是否下载了所有图像。 我的动机是,我想在所有图像下载后调用一个函数。如何知道AfNetworking是否已经下载了所有图像

我在使用UIImageview setImageWithURL for循环

感谢提前:)

+0

看我的答案在这里:http://stackoverflow.com/a/21832526/3319153 希望这将有助于。 –

回答

1

你要使用的UIImageView + AFNetworking的setImageWithURLRequest方法。当图像加载完成时,成功块会被执行。

@param urlRequest The URL request used for the image request. 
@param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the image view will not change its image until the image request finishes. 
@param success A block to be executed when the image request operation finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the image created from the response data of request. If the image was returned from cache, the request and response parameters will be `nil`. 
@param failure A block object to be executed when the image request operation finishes unsuccessfully, or that finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the error object describing the network or parsing error that occurred. 
*/ 
- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest 
       placeholderImage:(UIImage *)placeholderImage 
         success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success 
         failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure; 
+0

但我如何追踪上次下载的图片? – DAMM108

+0

问题是您正在加载n张图片,并且您想知道所有n张图片何时完成下载? – peterxv

+0

如果您只是试图确定n个图像何时完成下载。你可以创建一个实例变量downloadCount = 0,numImages = n。每当成功块执行增量downloadCount并且downloadCount == numImages,那么你就完成了。这可能是最简单的解决方案,但它会起作用。 – peterxv

1

由于setImageWithURL是异步的,这使得它有点难以跟踪的所有图像是否已被下载。可能有效的一种解决方案是使用setImageWithURLRequest:placeholderImage:success:failure:方法,该方法允许您在图像URL请求成功或失败时执行代码。

由于您正在运行for循环,因此您可能会有固定数量的图片通过。在这种情况下,您可以设置一个属性,用于跟踪已在success/failure块中下载的图像。当这个值等于你想要的数字时,你可以运行某种逻辑(即发布通知,委托)来触发所有下载已经完成。 (或者,如果有任何失败,则可以添加一些逻辑重试/发布消息说发生了错误等)

例如(假设numberOfImagesToDownload是一些恒定值集合):

- (void)processImageForURL:(NSURL *)url { 
    // Assume `placeholderImage` is a reference to an image. 
    [imageView setImageWithURLRequest:[NSURLRequest requestWithURL:url] 
        placeholderImage:placeholderImage 
           success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 
     // Logic here to set the imageView's image (for the example's sake, assume 
     // we have access to the respective UIImageView.) 
     imageView.image = image; 

     // Hold onto a property to keep track of how many images you've downloaded, 
     // under the assumption that there's a set number of images you need to download. 
     // Since you're running this under a for loop, you could probably check if the 
     // for's max condition is equal to the number of downloaded images. 
     self.numberOfImagesDownloaded++; 
     if(self.numberOfImagesDownloaded == numberOfImagesToDownload) { 
      // All images have been downloaded. 
     } 
    } 
    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { 
     // You can also keep track of which images failed, if that's important. 
    }]; 
} 
1

可以使用NSOperationQueue排队您的要求和使用KVO来观察你的队列的业务属性,那么就可以判断你的队列通过检查[queue.operations count] == 0完成。

添加的操作特性的观察员:

[self.queue addObserver:self forKeyPath:@"operations" options:0 context:NULL]; 

处理该事件时,操作计数命中0:

- (void) observeValueForKeyPath:(NSString *)keyPath 
         ofObject:(id)object 
         change:(NSDictionary *)change 
         context:(void *)context 
{ 
    if (object == self.queue && [keyPath isEqualToString:@"operations"]) 
    { 
     if ([self.queue.operations count] == 0) 
     { 
      // Your downloads have completed 
     } 
    } 
    else 
    { 
     [super observeValueForKeyPath:keyPath 
          ofObject:object 
           change:change 
           context:context]; 
    } 
} 

而且你把你的要求是这样的:

AFImageRequestOperation *imgRequest = [AFImageRequestOperation imageRequestOperationWithRequest:urlRequest success:^(UIImage *image) { } 

[self.queue addOperation: imgRequest] 

这只是一个伪代码。我没有测试过,但它应该指向正确的方向。

+0

AFnetworking中是否有全局队列?[只是好奇] – DAMM108

+0

@ DAMM108是的,如果你使用AFNetworking 1.x(它在AFHTTPClient上)或AFNetworking 2.x AFHTTPRequestOperationManager。但是,如果您在AFHTTPSessionManager中使用AFNetworking 2.x,则不会。 (好吧,还没有,它正在开发中。) –

+0

@Aaron Brager,我正在使用AFNetworking 1.x – DAMM108

相关问题