繁体   English   中英

无法将本地存储的图像更快地加载到集合视图中。?

[英]Unable to load locally stored images faster into collection view.?

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

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)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];
                   }


    });

}
});


 }

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

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

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

它会加载得更快,甚至会保存在缓存中

似乎当您从照片库中保存图像时,您应该根据需要保存完整尺寸,并保存可用于在应用程序中显示的适当缩放版本的图像。 如果使用ALAsset ,那可能是thumbnail

如果您这样做,图像加载将很快并且内存使用量最小。

目前,您只是试图将过多的图像数据加载到内存中,这需要很长时间,并且当它不可避免地用完时会导致应用程序崩溃。 用设备相机拍摄的图像实际上非常大......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM