我在收藏视图中显示了相册图片的数量。 对于在单元格方法中显示图像,cellForItemAtIndexPath包含如下代码。
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
AlbumImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AlbumImageCell" forIndexPath:indexPath];
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"AlbumImageCell" owner:self options:nil] objectAtIndex:0];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *inputPath = [[self.arrTbl_Album_Image objectAtIndex:indexPath.row]objectAtIndex:2 ];
NSString *imageEx=[inputPath pathExtension];
NSString *imageName=[[inputPath lastPathComponent]stringByDeletingPathExtension];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",imageName,imageEx]];
[cell.albumImage setImage:[UIImage imageWithContentsOfFile:fullPath]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data= [NSData dataWithContentsOfFile:fullPath];
UIImage *theImage=[UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
cell.albumImage.image=theImage;
});
});
}
问题是当我滚动时UICollection视图滚动工作缓慢,所以我需要使滚动更快。我应用一些解决方案,但仍然没有解决问题。 请帮我解决这个问题。
答案 0 :(得分:0)
[cell.albumImage setImage:[UIImage imageWithContentsOfFile:fullPath]]; // set image on main thread
&安培;
cell.albumImage.image=theImage;
两者相同
删除第一行代码
[cell.albumImage setImage:[UIImage imageWithContentsOfFile:FULLPATH]];
答案 1 :(得分:0)
NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *fullPath = [NSString stringWithFormat:@"%@/%@", docsDir,[filePathsArray objectAtIndex:indexPath.item]];
[cell.albumImage setImage:[UIImage imageWithContentsOfFile:fullPath]];
答案 2 :(得分:0)
您已使用[cell.albumImage setImage:[UIImage imageWithContentsOfFile:fullPath]];
他们为什么要使用下面的代码再次设置图像。您需要删除该代码。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data= [NSData dataWithContentsOfFile:fullPath];
UIImage *theImage=[UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
cell.albumImage.image=theImage;
});
});
答案 3 :(得分:0)
执行每个滚动
这是一项艰巨的任务NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *inputPath = [[self.arrTbl_Album_Image objectAtIndex:indexPath.row]objectAtIndex:2 ];
NSString *imageEx=[inputPath pathExtension];
NSString *imageName=[[inputPath lastPathComponent]stringByDeletingPathExtension];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",imageName,imageEx]];
你可以避免吗?让我们说,使用xcassets
?当然,它会更快。检索图像的后台操作根本不是问题,它位于后台。
已添加:您可以在此处查看如何使用xcassets
:https://developer.apple.com/library/ios/documentation/Xcode/Reference/xcode_ref-Asset_Catalog_Format/index.html#//apple_ref/doc/uid/TP40015170-CH18-SW1
答案 4 :(得分:0)
我遇到了同样的问题。当我分析应用程序(滚动时)时,我可以看到时间花在了AppleJPEG的某个地方(不记得确切的名字)。我的图像太大而且必须渲染得更小(对于单元格的大小)而且这恰好在主线程上。
尝试在后台线程上渲染图像。 查看:https://developer.apple.com/videos/play/wwdc2012-211/