如何知道AfNetworking是否已经下载了所有图像
我认为我在问一个愚蠢的问题,但只是好奇。 有什么方法可以知道是否下载了所有图像。 我的动机是,我想在所有图像下载后调用一个函数。如何知道AfNetworking是否已经下载了所有图像
我在使用UIImageview setImageWithURL
for循环
感谢提前:)
你要使用的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;
由于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.
}];
}
可以使用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]
这只是一个伪代码。我没有测试过,但它应该指向正确的方向。
看我的答案在这里:http://stackoverflow.com/a/21832526/3319153 希望这将有助于。 –