当我拍照时,我将图像文件保存到我在 NSDocuments 目录中创建的文件夹中。(/文档/照片/..png)
然后我在收藏视图的照片文件夹中加载所有照片。由于文件很大,我决定使用 Grand Central 调度在后台线程上执行抓取图像。但是当我向上和向下滚动 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;
}