iphone - 在 Collection View 中加载图像的更快方法

标签 iphone ios objective-c cocoa-touch

我的 UICollectionView 有一些流程问题。我想显示 PDF 的缩略图,就像在 Apple 的 iBook 应用程序中一样,当我滚动我的收藏夹 View 时,我发现它不太平滑。这是我用来加载图片的方式:

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:    (NSIndexPath *)indexPath
{
     GridCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"gridCell" forIndexPath:indexPath];

    ...

    // Set Thumbnail
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        if ([Tools checkIfLocalFileExist:cell.pdfDoc])
        {
            UIImage *thumbnail = [Tools generateThumbnailForFile:((PDFDocument *)[self.pdfList objectAtIndex:indexPath.row]).title];

            dispatch_async( dispatch_get_main_queue(), ^{
                [cell.imageView setImage:thumbnail];
            });
        }
    });

    ...

    return cell;
}

获取缩略图的方法:

+ (UIImage *)generateThumbnailForFile:(NSString *) fileName
{
    // ----- Check if thumbnail already exist
    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString* thumbnailAddress = [documentsPath stringByAppendingPathComponent:[[fileName stringByDeletingPathExtension] stringByAppendingString:@".png"]];
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:thumbnailAddress];

    if (fileExists)
        return [UIImage imageWithContentsOfFile:thumbnailAddress];

    // ----- Generate Thumbnail
    NSString* filePath = [documentsPath stringByAppendingPathComponent:fileName];

    CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:filePath];
    CGPDFDocumentRef documentRef = CGPDFDocumentCreateWithURL(url);
    CGPDFPageRef pageRef = CGPDFDocumentGetPage(documentRef, 1);
    CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox);

    UIGraphicsBeginImageContext(pageRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, CGRectGetMinX(pageRect),CGRectGetMaxY(pageRect));
    CGContextScaleCTM(context, 1, -1);
    CGContextTranslateCTM(context, -(pageRect.origin.x), -(pageRect.origin.y));
    CGContextDrawPDFPage(context, pageRef);

    // ----- Save Image
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(finalImage)];
    [imageData writeToFile:thumbnailAddress atomically:YES];
    UIGraphicsEndImageContext();

    return finalImage;    
}

你有什么建议吗?

最佳答案

看看https://github.com/rs/SDWebImage 。该库非常适合异步图像加载,特别是方法 setImageWithURL:placeholderImage:

您可以调用该方法并使用加载图像或空白 png 设置占位符,一旦您尝试检索的图像加载,它将填充占位符。这应该会大大加快您的应用程序的速度。

关于iphone - 在 Collection View 中加载图像的更快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16736306/

相关文章:

jquery - iPhone iOS7 3D 选择/下拉功能

ios - 跟踪 iPhone 上的多次触摸

ios - 可重复使用的自定义单元格

objective-c - 将 char 输入复制到字符串数组中

objective-c - 动画行删除后重新加载表

iphone - 应用程序未出现在设置中的 "Notifications"下

ios - 是否可以暂停和恢复 AVPlayer 的缓冲?

objective-c - 调整 UITableViewCell 的 contentView 大小时 UILabel 的过渡效果

ios - 使用同一按钮播放/暂停

ios - UIButton 在 TableView 单元格中不可见