代码之家  ›  专栏  ›  技术社区  ›  Monika Patel

从文档目录加载多个图像时,UICollectionView卡住

  •  0
  • Monika Patel  · 技术社区  · 8 年前

    我正在加载超过30-50个图像,这些图像存储在文档目录中。所有图像都会立即加载到 collectionView ,但当滚动时 集合视图 到那时它会卡住的。

    - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellIdentifier = @"ImageCell";
        ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    
        //Load image from document  directory
        NSString *imageName = [arrCollection objectAtIndex:indexPath.row];
        NSString *imagePath = [NSString stringWithFormat:@"%@/%@",(AppObj).imagefolderPath, imageName];
        UIImage *localImg = [[UIImage alloc]initWithContentsOfFile:imagePath];
    
        cell.imgSketch.image = localImg;
    
        cell.txtName.text = [NSString stringWithFormat:@"%@",[imageName stringByReplacingOccurrencesOfString:@".png" withString:@""]];
    
        cell.layer.masksToBounds = YES;
        return cell;
    }
    

    我也试着跟踪调度组

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
                    UIImage *localImg = [[UIImage alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",(AppObj).imagefolderPath,[arrCollection objectAtIndex:indexPath.row]]];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        cell.imgSketch.image = localImg;
                    });
                });
    

    请给我建议解决这个问题的办法。

    提前谢谢

    4 回复  |  直到 8 年前
        1
  •  1
  •   Nirav D    8 年前

    你可以用 SDWebImage 它还显示来自文档目录的图像。

    [cell.imgSketch sd_setImageWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",(AppObj).imagefolderPath,[arrCollection objectAtIndex:indexPath.row]]]];
    

    注: 使用 fileURLWithPath 创建对象时 NSURL 因为它是文件的路径。

        2
  •  0
  •   bseh    8 年前

    我建议将图像加载操作移到后台线程。

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL), ^{
       UIImage* localImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",(AppObj).imagefolderPath,[arrCollection objectAtIndex:indexPath.row]]];    
       dispatch_async(dispatch_get_main_queue(), ^{
          cell.imgSketch.image = localImage;
       });
    });
    
        3
  •  0
  •   Shehata Gamal    8 年前

    我建议只加载一次所有的图像 cellForRowAt

    NSMutableArray*images; // every instance is of type UIImage not imageName
    

    而不是这个

    UIImage *localImg = [[UIImage alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",(AppObj).imagefolderPath,[arrCollection objectAtIndex:indexPath.row]]];
    

    注: 这种方法 initWithContentsOfFile 在主队列中运行,所以这是卡滞的主要原因

        4
  •  0
  •   Lal Krishna    8 年前

    如果使用大图像,collectionview/tableview单元格将闪烁。尝试使用小尺寸图像或在 cellForItemAtIndexPath 在后台队列中。

    可以通过在数组中缓存图像来提高性能。 注意:如果容量增加了限制,请尝试清洁它。

    cell.imgSketch.image = nil;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    
         UIImage *localImg = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", (AppObj).imagefolderPath, [arrCollection objectAtIndex:indexPath.row]]];
    
         NSData *imgData = UIImageJPEGRepresentation(localImg, 0.5);
         localImg = [UIImage imageWithData:imgData];
    
         dispatch_async(dispatch_get_main_queue(), ^{
             ImageCell *imageCell = [collectionView cellForItemAtIndexPath:indexPath];
             if (imageCell) {
                 cell.imgSketch.image = localImg;
             }
        });
    });