Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何知道AfNetworking是否下载了所有图像_Ios_Ios6_Afnetworking - Fatal编程技术网

Ios 如何知道AfNetworking是否下载了所有图像

Ios 如何知道AfNetworking是否下载了所有图像,ios,ios6,afnetworking,Ios,Ios6,Afnetworking,我想我在问一个愚蠢的问题,但只是好奇而已。 有没有办法知道所有的图片都下载了。 我这样做的动机是,我想在下载完所有图像后调用一个函数 我正在for循环中使用UIImageview setImageWithURL 提前感谢:)您需要使用UIImageView+AFNetworking的setImageWithURLRequest方法。当映像完成加载时,将执行成功块 @param urlRequest The URL request used for the image request. @pa

我想我在问一个愚蠢的问题,但只是好奇而已。 有没有办法知道所有的图片都下载了。 我这样做的动机是,我想在下载完所有图像后调用一个函数

我正在for循环中使用
UIImageview setImageWithURL


提前感谢:)

您需要使用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:Placeholder图像:成功:失败:
方法,该方法允许您在图像URL请求成功或失败时执行代码

因为您正在运行一个
for
循环,所以您可能有固定数量的图像要运行。在这种情况下,您可以设置一个属性来跟踪在
成功
/
失败
块中下载的图像。当该值等于您想要的数字时,您可以运行某种逻辑(即发布通知、委派)来触发所有下载已完成。(或者,如果出现任何故障,您可以添加一些逻辑,用于重试/发布消息,说明出现错误等。)

例如(假设
numberOfImagesToDownload
是某个常量值集):


您可以使用
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]

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

请看我的回答:希望这会有所帮助。但我如何跟踪上次下载的图像?问题是您正在加载n个图像,并且您想知道所有n个图像何时完成下载?如果您只是试图确定n个图像何时完成下载。您可以创建一个实例变量downloadCount=0,numImages=n。每次成功块执行增量downloadCount时,如果downloadCount==numImages,则完成。这可能是最简单的解决方案,但它会起作用。我正在考虑的同一个解决方案也会尝试:)@DAMM108是的,如果您使用AFHTTPClient上的AFNetworking 1.x或AFHTTPRequestOperationManager上的AFNetworking 2.x。但是,如果您将AFNetworking 2.x与AFHTTPSessionManager一起使用,则不会(还没有,它正在开发中。)AFHTTPClient操作队列
AFImageRequestOperation *imgRequest = [AFImageRequestOperation imageRequestOperationWithRequest:urlRequest success:^(UIImage *image) { }

[self.queue addOperation: imgRequest]