AFNetworking setImageWithURLRequest download progress

拥有回忆 提交于 2019-12-10 23:23:51

问题


I am using this code to set image to UIImageView.

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
    }];

But I want to track download progress with that method, is it possbile to do it in setImageWithURLRequest method?

Normally I do this to show loading progress percentage:

[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)]];
    }];

回答1:


Out of the box, no UIImageView+AFNetworking category doesn't have this functionality. However, it can easily be added to by adding this method to the category:

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



回答2:


Take a look at this cocoa pod: https://github.com/xmartlabs/XLRemoteImageView . It uses objective-c internals to achieve what you want. I hope it helps you.



来源:https://stackoverflow.com/questions/13821428/afnetworking-setimagewithurlrequest-download-progress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!