代码之家  ›  专栏  ›  技术社区  ›  user6092898

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

  •  0
  • user6092898  · 技术社区  · 8 年前

    我有一个 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. 有没有其他更好的方法可以使用本机方法将图像从url直接加载到UIImage中?

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

    NSData *storeImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:productImageArray[indexPath.row]]];
    
            self.productImage.image = [UIImage imageWithData:storeImageData];
    
    3 回复  |  直到 8 年前
        1
  •  1
  •   Julien Quere    8 年前

    @安布。卡提克的回答是正确的。

    但是,也许最简单的解决方案是使用类似 SDWebImage 不这个库将处理这个问题以及更多(缓存、错误管理、正确的表视图单元格处理…)。

    我认为你至少应该花点时间看看: https://github.com/rs/SDWebImage

    编辑:

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

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

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

        2
  •  0
  •   Anbu.Karthik    8 年前

    试试这个

    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];
    
        3
  •  -1
  •   Bhikhu Vaja    8 年前
    NSString *url_Img1 = @"Your url";   
      Uiimageview *view_Image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url_Img1]]];