【问题标题】:AFNetworking 3.0 setImageWithURLRequest download progressAFNetworking 3.0 setImageWithURLRequest 下载进度
【发布时间】:2016-01-12 12:42:50
【问题描述】:

在使用UIImageView+AFNetworking 类别和AFNetworking 3.0 时,有没有人有一个很好的工作解决方案来获取下载进度。

这个category,我在 3.0 之前的版本中使用过,现在已经停止工作。

这是我自己的实验性version,可悲的是,目前它随机崩溃。

【问题讨论】:

  • 你检查过这个吗 -> github.com/xmartlabs/XLRemoteImageView
  • 请参考我的回答。我已经向 AFNetworking 提出了拉取请求,不幸的是它被拒绝了。在那之前,如果您不想更改/升级您的 AFNetworking 版本,您可以使用它。
  • 这是一个很好的努力@iOSEnthusiatic 。顺便说一句,几天前我也给了他们一个 pullrequest github.com/AFNetworking/AFNetworking/pull/3306

标签: ios uiimageview afnetworking progress afnetworking-3


【解决方案1】:

这是 AFNetworking 3.0 的修改版本,您可以在其中使用 UIImageView+AFNetworking 类别显示从服务器加载图像的进度。

https://github.com/rushisangani/AFNetworking

请将以下文件替换为原始 AFNetworking 文件。

UIImageView+AFNetworking.h,
UIImageView+AFNetworking.m,

UIImage+ImageDownloader.h,
UIImage+ImageDownloader.m

注意:如果您更新您的 pod,那么这将被删除。

【讨论】:

    【解决方案2】:

    如果您查看 AFImageDownloader 这个,它被 UIImageView 类别用来下载图像。在这堂课中你

    - (nullable AFImageDownloadReceipt *)downloadImageForURLRequest:  (NSURLRequest *)request
                                                        success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse  * _Nullable response, UIImage *responseObject))success
                                                        failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure;
    

    它返回一个带有 NSURLSessionDataTask *task 属性的收据。 NSURLSessionDataTask 具有不同的字节下载方式、预期字节接收等属性。也许你可以使用它来完成你的任务。

    【讨论】:

      【解决方案3】:

      您可以通过在 UIImageView+AFNetworking.h

      中添加几行来实现此目的
      1. 将此代码放在导入语句下的文件顶部

        static void * AFTaskCountOfBytesReceivedContext = &AFTaskCountOfBytesReceivedContext;
        
      2. 你需要注册观察者来跟踪接收到的字节,方法是在函数setImageWithURLRequest下添加下面的行

        if (cachedImage) {
            // AFNetworking default code
        }
        else{
            // AFNetworking default code
            // Our new lines to track the download
            [self.af_activeImageDownloadReceipt.task addObserver:self forKeyPath:@"state" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesReceivedContext];
            [self.af_activeImageDownloadReceipt.task addObserver:self forKeyPath:@"countOfBytesReceived" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesReceivedContext];
        }
        
      3. 在末尾添加这个新功能。

        #pragma mark - NSKeyValueObserving
        
        - (void)observeValueForKeyPath:(NSString *)keyPath
                              ofObject:(id)object
                                change:(__unused NSDictionary *)change
                               context:(void *)context
        {
            if (context == AFTaskCountOfBytesReceivedContext) {
        
                if ([keyPath isEqualToString:NSStringFromSelector(@selector(countOfBytesReceived))]) {
                    if ([object countOfBytesExpectedToReceive] > 0) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            //You can do your stuff at here like show progress
                            NSLog(@"Progress : %f",[object countOfBytesReceived] / ([object countOfBytesExpectedToReceive] * 1.0f));
        
                        });
                    }
                }
        
                if ([keyPath isEqualToString:NSStringFromSelector(@selector(state))]) {
                    if ([(NSURLSessionTask *)object state] == NSURLSessionTaskStateCompleted) {
                        @try {
                            [object removeObserver:self forKeyPath:NSStringFromSelector(@selector(state))];
                            NSLog(@"Image Download Complete");
                            if (context == AFTaskCountOfBytesReceivedContext) {
                                [object removeObserver:self forKeyPath:NSStringFromSelector(@selector(countOfBytesReceived))];
                            }
                        }
                        @catch (NSException * __unused exception) {}
                    }
                }
            }
        }
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多