【问题标题】:Loading images from file to UICollectionView, using GCD使用 GCD 将图像从文件加载到 UICollectionView
【发布时间】: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


【解决方案1】:

查看 Apple 的示例应用:LazyTableImages。我刚刚成功地将它合并到一个集合视图中。

http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 2016-10-13
    相关资源
    最近更新 更多