【发布时间】:2013-04-09 05:01:49
【问题描述】:
当我拍照时,我将图像文件保存到我在 NSDocuments 目录中创建的文件夹中。 (/Documents/Photos/..png)
然后我在收藏视图中加载照片文件夹中的所有照片。由于文件很大,我决定使用 Grand Central dispatch 在后台线程上执行抓取图像。但是当我向上和向下滚动 UICollectionView 时,我可以看到图像在显示正确图像之前随机变化。我假设每当我抓取图像时,我只是将它设置在主线程上,但我真的不知道如何处理这个问题。
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger rowNumber = [indexPath row];
//NSString *imagePath = [self.photoPathArray objectAtIndex:rowNumber];
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"Photos"] error:NULL];
NSString *imagePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/Photos/%@",[directoryContent objectAtIndex:rowNumber]];
NSLog(@"the image path is %@", imagePath);
imageCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"imageCell" forIndexPath:indexPath];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *originalImage = [UIImage imageWithContentsOfFile:imagePath];
UIImage *thumbNail = [self shrinkImage:originalImage withSize:CGSizeMake(200, 200)];
dispatch_sync(dispatch_get_main_queue(), ^{
cell.imageView.image = thumbNail;
});
});
return cell;
}
【问题讨论】:
-
你从哪里得到变量
rowNumber?你能发布整个cellForItemAtIndexPath方法吗? -
嗨,博格丹,刚刚发布。
-
@Jomoka 你解决了吗?我遇到过同样的问题,但在其他类似帖子中找不到真正的解决方案。
标签: ios grand-central-dispatch uicollectionview