• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 无法将本地存储的图像更快地加载到 Collection View 中。?

[复制链接]
菜鸟教程小白 发表于 2022-12-13 15:41:22 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有多个图像和视频存储在应用程序文档目录中。我以这种方式使用 SDWebImage 在 Collection View 中显示图像和视频。

-(UICollectionViewCell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)indexPath{


AlbumImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier"AlbumImageCell" forIndexPath:indexPath];
cell.albumImage.image=nil;
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
if (cell == nil) {
    cell = [[[NSBundle  mainBundle] loadNibNamed"AlbumImageCell" owner:self options:nil] objectAtIndex:0];
}

NSInteger Type=[[[self.arrTbl_Album_Image objectAtIndex:indexPath.row] objectAtIndex:3] intValue];
if (Type==1) {
        cell.imgPlay.hidden=YES;
        [cell.albumImage setImage:[images objectAtIndex:indexPath.row]];
    }else{
        cell.imgPlay.hidden=NO;
        [cell.albumImage setImage:[images objectAtIndex:indexPath.row]];
    }

    return cell;
}

这里是生成缩略图的代码。

-(void)load_Album_Data{
       NSUserDefaults *album_id=[NSUserDefaults standardUserDefaults];
       NSInteger Album_id=[album_id integerForKey"album_Id"];

//From the query.
NSString *query=[NSString stringWithFormat"select * from tbl_Album_Image where album_id='%ld' order by id desc",Album_id];


//Get result.
if (self.arrTbl_Album_Image != nil) {
    self.arrTbl_Album_Image=nil;
}
self.arrTbl_Album_Image=[[NSArray alloc]initWithArray:[self.dbManager loadDataFromDB:query]];


dispatch_queue_t myQueue = dispatch_queue_create("MyQueue",NULL);
dispatch_async(myQueue, ^{

for(int i=0;i<self.arrTbl_Album_Image.count;i++)
{
    NSInteger AssetType=[[[self.arrTbl_Album_Image objectAtIndex:i] objectAtIndex:3] intValue];
    if (AssetType==1) {
       //for image 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *inputPath = [[self.arrTbl_Album_Image objectAtIndex:i]objectAtIndex:2 ];

    NSString *imageEx=[inputPath pathExtension];
    NSString *imageName=[[inputPath lastPathComponent]stringByDeletingPathExtension];

    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat"%@.%@",imageName,imageEx]];

    UIImage *img=[UIImage imageWithContentsOfFile:fullPath];
    CGFloat scale = MAX(106/img.size.width, 106/img.size.height);
    CGFloat width = img.size.width * scale;
    CGFloat height = img.size.height * scale;
    CGRect imageRect = CGRectMake((106 - width)/2.0f,
                                  (106 - height)/2.0f,
                                  width,
                                  height);
    CGSize c=CGSizeMake(106, 106);
    UIGraphicsBeginImageContextWithOptions(c, NO, 0);
    [img drawInRect:imageRect];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();


    [images addObject:newImage];
    }
    else{
        //for video 
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *inputPath = [[self.arrTbl_Album_Image objectAtIndex:i]objectAtIndex:2 ];

        NSString *imageEx=[inputPath pathExtension];
        NSString *imageName=[[inputPath lastPathComponent]stringByDeletingPathExtension];

        NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat"%@.%@",imageName,imageEx]];

        NSURL *videoURL = [NSURL fileURLWithPath:fullPath];
        AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];

        AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
        imageGenerator.appliesPreferredTrackTransform = YES;
        NSError *err = NULL;
        CMTime time = CMTimeMake(1, 2);
        CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:&err];
        UIImage *thumbNailImage = [[UIImage alloc] initWithCGImage:imageRef];


        CGFloat scale = MAX(106/thumbNailImage.size.width, 106/thumbNailImage.size.height);
        CGFloat width = thumbNailImage.size.width * scale;
        CGFloat height = thumbNailImage.size.height * scale;
        CGRect imageRect = CGRectMake((106 - width)/2.0f,
                                      (106 - height)/2.0f,
                                      width,
                                      height);
        CGSize c=CGSizeMake(106, 106);
        UIGraphicsBeginImageContextWithOptions(c, NO, 0);
        [thumbNailImage drawInRect:imageRect];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

             [images addObject:newImage];

    }

    dispatch_async(dispatch_get_main_queue(), ^{
             //Reload the Image view.
     [self.album_ImageView reloadData];
                   }


    });

}
});


 }

但问题是我无法更快地加载图像,并且当快速上下滚动时应用程序崩溃。并且对于未生成的视频图像也是如此。 请帮我解决这个问题并为我提供任何解决方案。 我希望在 Collection View 单元格中快速加载图像。



Best Answer-推荐答案


而不是你的代码来设置图像更改

 [cell.albumImage sd_setImageWithURL:imaURL placeholderImage:[UIImage imageNamed"placeholder.png"]  options:SDWebImageRefreshCached   completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

        dispatch_async(dispatch_get_main_queue(), ^{

            cell.albumImage.image=image;
        });
    }];

加载速度更快,甚至可以保存在缓存中

关于ios - 无法将本地存储的图像更快地加载到 Collection View 中。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36262034/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap