Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在“集合”视图中加载图像的更快方法_Iphone_Ios_Objective C_Cocoa Touch - Fatal编程技术网

Iphone 在“集合”视图中加载图像的更快方法

Iphone 在“集合”视图中加载图像的更快方法,iphone,ios,objective-c,cocoa-touch,Iphone,Ios,Objective C,Cocoa Touch,我的UICollectionView有点流量问题。我想显示PDF的缩略图,就像在苹果的iBook应用程序中一样,当我滚动我的收藏视图时,我可以看到它不是很平滑。以下是我加载图片的方式: - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath: (NSIndexPath *)indexPath { GridCell *cell = [cv dequeueReusab

我的UICollectionView有点流量问题。我想显示PDF的缩略图,就像在苹果的iBook应用程序中一样,当我滚动我的收藏视图时,我可以看到它不是很平滑。以下是我加载图片的方式:

- (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;    
}
你有什么建议吗?

看看。该库非常适合异步图像加载,尤其是方法
setImageWithURL:placeholder图像:


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

你考虑过缓存(存储)缩略图吗?是的,我已经这么做了!谢谢你的图书馆,我来看看。