我想我问的是一个愚蠢的问题,但只是好奇。 有没有办法知道是否下载了所有图像。 我的动机是我想在下载所有图像后调用一个函数。
我在for循环中使用UIImageview setImageWithURL
提前致谢:)
答案 0 :(得分: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;
答案 1 :(得分: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.
}];
}
答案 2 :(得分: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]
这只是一个伪代码。我没有测试它,但它应该指向正确的方向。