ios - 无法将本地存储的图像更快地加载到 Collection View 中。?
<p><p>我有多个图像和视频存储在应用程序文档目录中。我以这种方式使用 SDWebImage 在 Collection View 中显示图像和视频。</p>
<pre><code>-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
AlbumImageCell *cell = ;
cell.albumImage.image=nil;
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = .scale;
if (cell == nil) {
cell = [[ loadNibNamed:@"AlbumImageCell" owner:self options:nil] objectAtIndex:0];
}
NSInteger Type=[[ objectAtIndex:3] intValue];
if (Type==1) {
cell.imgPlay.hidden=YES;
];
}else{
cell.imgPlay.hidden=NO;
];
}
return cell;
}
</code></pre>
<p>这里是生成缩略图的代码。</p>
<pre><code>-(void)load_Album_Data{
NSUserDefaults *album_id=;
NSInteger Album_id=;
//From the query.
NSString *query=;
//Get result.
if (self.arrTbl_Album_Image != nil) {
self.arrTbl_Album_Image=nil;
}
self.arrTbl_Album_Image=[initWithArray:];
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=[[ objectAtIndex:3] intValue];
if (AssetType==1) {
//for image
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = ;
NSString *inputPath = [objectAtIndex:2 ];
NSString *imageEx=;
NSString *imageName=[stringByDeletingPathExtension];
NSString *fullPath = ];
UIImage *img=;
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);
;
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
;
}
else{
//for video
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = ;
NSString *inputPath = [objectAtIndex:2 ];
NSString *imageEx=;
NSString *imageName=[stringByDeletingPathExtension];
NSString *fullPath = ];
NSURL *videoURL = ;
AVURLAsset *asset = [ initWithURL:videoURL options:nil];
AVAssetImageGenerator *imageGenerator = [ initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
CMTime time = CMTimeMake(1, 2);
CGImageRef imageRef = ;
UIImage *thumbNailImage = [ 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);
;
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
;
}
dispatch_async(dispatch_get_main_queue(), ^{
//Reload the Image view.
;
}
});
}
});
}
</code></pre>
<p>但问题是我无法更快地加载图像,并且当快速上下滚动时应用程序崩溃。并且对于未生成的视频图像也是如此。
请帮我解决这个问题并为我提供任何解决方案。
我希望在 Collection View 单元格中快速加载图像。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>而不是你的代码来设置图像更改</p>
<pre><code> options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
dispatch_async(dispatch_get_main_queue(), ^{
cell.albumImage.image=image;
});
}];
</code></pre>
<p>加载速度更快,甚至可以保存在缓存中</p></p>
<p style="font-size: 20px;">关于ios - 无法将本地存储的图像更快地加载到 Collection View 中。?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36262034/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36262034/
</a>
</p>
页:
[1]