gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:13:50 24 4
gpt4 key购买 nike

我的 UICollectionView 有一点流程问题。我想像在 Apple 的 iBook 应用程序中一样显示 PDF 的缩略图,当我滚动我的收藏 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com