2012-12-11 90 views
2

我正在使用此代碼將圖像設置爲UIImageView。AFNetworking setImageWithURLRequest下載進度

NSURLRequest *URLRequest = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:imageToLoad]]; 

    [imageView setImageWithURLRequest:URLRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 
     [cell.image setImage:image]; 
     [cell.activityIndicator stopAnimating]; 

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { 
     // Nothing 
    }]; 

但我想跟蹤與方法下載進度,是possbile做到在setImageWithURLRequest方法?

通常我這樣做是爲了顯示加載進度百分比:

[SVProgressHUD showWithStatus:@"Start download..."]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:link]]; 
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
     // success 
     [SVProgressHUD showSuccessWithStatus:@"Done."]; 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     // failed 
     [SVProgressHUD showErrorWithStatus:@"Failed."]; 
    }]; 

    [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) { 
     [SVProgressHUD showWithStatus:[NSString stringWithFormat:@"Downloading... %0.0f%%", totalBytesRead*100*1.0/(totalBytesRead+totalBytesExpectedToRead)]]; 
    }]; 
+0

我已經回答了這個問題在[http://stackoverflow.com/questions/34744024/afnetworking-3-0-setimagewithurlrequest-download-progress/36983966#36983966](http://stackoverflow.com/questions/34744024/afnetworking-3-0-setimagewithurlrequest-download-progress/36983966#36983966) – Jegan

回答

2

開箱即用,沒有的UIImageView + AFNetworking類別不具有此功能。但是,它可以很容易地通過增加這種方法的類別添加到:

-(void)setDownloadProgressBlock:(void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))block{ 
    [self.af_imageRequestOperation setDownloadProgressBlock:block]; 
} 
+0

你能解釋一下怎麼做嗎? – 1337code

+0

注意:截至2013年5月,看起來你*可以*實際上只是調用'setDownloadProgressBlock' - 不需要該類別 – taber