9

UIImageView+AFNetworkingカテゴリとAFNetworking 3.0を使用するときに、ダウンロードの進行状況を取得するための優れた実用的なソリューションはありますか?

3.0 までのバージョンで使用していたこのカテゴリは、現在は機能しなくなりました。

これは私自身の実験的なバージョンで、悲しいことに、現時点ではランダムにクラッシュします。

4

3 に答える 3

1

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 には、バイトのダウンロード方法、予想される受信バイト数などのさまざまなプロパティがあります。おそらく、これを使用してタスクを達成できます。

于 2016-01-19T07:15:34.463 に答える
1

これは、 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) {}
                }
            }
        }
    }
    
于 2016-05-02T13:44:40.137 に答える