将图像从 Web 服务加载到 UIImage 无法正常工作

Loading an image from web-service on to an UIImage doesn't work properly

我有一个 productImageArray,其中包含 url 作为数组的元素。 我正在尝试在图像视图中加载那些 url。

以下是我加载它的方式。

UIActivityIndicatorView *spinner=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        spinner.center=CGPointMake(160.0,240.0 );
        spinner.hidesWhenStopped=YES;
        [self.view addSubview:spinner];
        [spinner startAnimating];

        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
        dispatch_async(queue, ^{
             NSData *storeImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:productImageArray[indexPath.row]]];
            self.productImage.image = [UIImage imageWithData:storeImageData];
            dispatch_sync(dispatch_get_main_queue(), ^{

                [spinner stopAnimating];
            });
        });

问题是,

  1. 只有我的表格视图的最后一个单元格加载图像,而其余单元格不加载 url
  2. 中的图像
  3. 有没有其他更好的方法可以使用本机方法将图像从 url 直接加载到我的 UIImage 中?

当我使用以下代码时,我的表格视图的每个单元格都加载了图像数据,但它仍然冻结用户界面,直到数据完全加载

NSData *storeImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:productImageArray[indexPath.row]]];

        self.productImage.image = [UIImage imageWithData:storeImageData];

试试这个

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
    dispatch_async(queue, ^{
         NSData *storeImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:productImageArray[indexPath.row]]];

        dispatch_sync(dispatch_get_main_queue(), ^{

            [spinner stopAnimating];
              self.productImage.image = [UIImage imageWithData:storeImageData];
        });
    });

或者试试

self.productImage.image = nil;   //// [UIImage imageNamed:@"default.png"];

 NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:productImageArray[indexPath.row]] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    if (data) {
        UIImage *currentImage = [UIImage imageWithData:data];
        if (currentImage) {
            dispatch_async(dispatch_get_main_queue(), ^{
                UITableviewCell *getCurrentCell = (id)[tableView cellForRowAtIndexPath:indexPath];
                if (getCurrentCell)
                     self.productImage.image = currentImage;
            });
        }
    }
}];
[task resume];

@Anbu.Karthik回答正确

但是,也许最简单的解决方案是使用 SDWebImage 之类的东西,不是吗?这个库将处理这个问题以及更多(缓存、错误管理、适当的 tableview 单元格处理,...)。

我认为你至少应该花几分钟时间看一下:https://github.com/rs/SDWebImage

编辑:

如果您使用 SDWebImageUIActivityIndicator-for-SDWebImage,您可以将整个代码替换为:

   [self.productImage setImageWithURL:[NSURL URLWithString:productImageArray[indexPath.row]]
                      usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

有关 UIActivityIndicator-for-SDWebImage 的更多信息:https://github.com/JJSaccolo/UIActivityIndicator-for-SDWebImage

NSString *url_Img1 = @"Your url";   
  Uiimageview *view_Image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url_Img1]]];