Iphone 从文档目录加载图像时,UITableView滚动延迟

Iphone 从文档目录加载图像时,UITableView滚动延迟,iphone,uitableview,uiimage,scroll,lag,Iphone,Uitableview,Uiimage,Scroll,Lag,我有一个UITableView,它显示带有图像和名称的项目。问题是,由于每个单元格都加载了图像,所以滚动会滞后 这就是我如何保存从iPhone摄像头捕获的图像,并将其大小调整为320 x 480 - (void)saveImage: (UIImage*)image{ if (image != nil) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,

我有一个UITableView,它显示带有图像和名称的项目。问题是,由于每个单元格都加载了图像,所以滚动会滞后

这就是我如何保存从iPhone摄像头捕获的图像,并将其大小调整为320 x 480

- (void)saveImage: (UIImage*)image{
if (image != nil)
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *imageName = [NSString stringWithFormat: @"image_%@.png",[NSDate date]];
    item.imageName = imageName;

    NSString* path = [documentsDirectory stringByAppendingPathComponent: 
                      imageName];
    NSLog(@"%@",path);

    NSData* data = UIImagePNGRepresentation(image);
    [data writeToFile:path atomically:YES];

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setInteger:[image imageOrientation] forKey:@"kImageOrientation"];
} }
这就是我如何调整它的大小

- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
CGImageRef imageRef = image.CGImage;

UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();

// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

CGContextConcatCTM(context, flipVertical);  
// Draw into the context; this scales the image
CGContextDrawImage(context, newRect, imageRef);

// Get the resized image from the context and a UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

CGImageRelease(newImageRef);
UIGraphicsEndImageContext();    

return newImage;}
这就是我在CellForRowatineXpath中加载它们的方式

- (UIImage*)loadImage:(NSString *) imageName{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                     NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent: 
                  [NSString stringWithString: imageName]];
    UIImage* image = [UIImage imageWithContentsOfFile:path];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    UIImage *orientedImage= [UIImage imageWithCGImage: image.CGImage scale:1.0 orientation:  [userDefaults integerForKey:@"kImageOrientation"]];

return orientedImage;}
我想到的一种方法是让NSMutableArray加载一次UIImages,然后从中获取图像。该方法的问题是,并非所有单元格都有图像,因此假设有10个单元格,但在最后3个单元格中只有3个图像,NSMutableArray方法将是一个问题,因为添加的图像将位于索引0,1,2处


我脑海中还有一个解决方案,我想在这里强调一下,供各位专家验证它是否有效。实际上,我正在使用CoreData并为该项目生成了模型。如果我要在此类中手动添加UIImage字段并将图像保存到对象中,那么这样做会有效还是错误?

使用延迟加载,请检查此选项。似乎上述解决方案适用于通过网络下载的图像。我正在从文档目录加载图像。您的解决方案是否也适用于此?谢谢。是的,你可以。这就像在后台(网络或本地)加载图像一样,一旦图像准备好就放在手机上。我会问另一个问题,因为我在我写的一个应用程序中看到了这种情况。我知道您说要将图像大小调整为320 x 480,但在UITableView上显示的图像大小是多少?我最初是按照你说的做的,并将图像加载到一个数组中,但是因为来自源的图像本身需要在每个单元格的UIImageView中显示之前调整大小,所以表视图滞后。我完全同意延迟加载,因为它将加载放在不同的线程上。因为您的图像在设备上,而不是通过慢速3G类型的连接,所以您可以简单地推迟显示该视图,直到您的文件全部加载完毕,然后使用微调器显示您正在工作。当然,在用户界面设计方面,我永远不会让用户访问视图,直到它在房子的另一边完全可用。如果图像是界面的一部分,请阻止用户,直到界面完成加载。