Iphone UITableView由于处理而滚动不平滑
我使用UITableView在每个表视图单元格中显示2到3个图像作为缩略图。我在tableview中显示的图像是巨大的。如果我直接使用这些图像而没有任何大小缩减,那么如果我在tableview和其他视图控制器之间来回移动,tableview可能会丢失一些图像 所以,我想到了缩小图像大小的代码。下面给出的代码非常有效,之前的问题已经解决。但是tableview会失去它的平滑滚动,因为大小缩减代码使用了大量内存。每次显示新的tableview单元格时,它都会处理(减小大小)特定tableview单元格中的图像 对此可能有一个简单的解决方案。提前谢谢 //尺寸缩减代码Iphone UITableView由于处理而滚动不平滑,iphone,uitableview,ios4,uiimage,Iphone,Uitableview,Ios4,Uiimage,我使用UITableView在每个表视图单元格中显示2到3个图像作为缩略图。我在tableview中显示的图像是巨大的。如果我直接使用这些图像而没有任何大小缩减,那么如果我在tableview和其他视图控制器之间来回移动,tableview可能会丢失一些图像 所以,我想到了缩小图像大小的代码。下面给出的代码非常有效,之前的问题已经解决。但是tableview会失去它的平滑滚动,因为大小缩减代码使用了大量内存。每次显示新的tableview单元格时,它都会处理(减小大小)特定tableview单元
CGSize newSize=CGSizeMake(_new_width, _new_height);
UIGraphicsBeginImageContext( newSize );
[sd._image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
scene_image_preview.image =newImage;
在缩小图像的基础上,您可以将生成的新图像作为文件存储在应用程序的documents文件夹中
NSData *imageData = UIImagePNGRepresentation([UIImage imageWithCGImage:newImage]);
CGImageRelease(newImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [NSString stringWithFormat:@"%@/%@%d_%d.png",documentsDirectory, prefix, x, y];
[imageData writeToFile:path atomically:NO];
在缩小图像的基础上,您可以将生成的新图像作为文件存储在应用程序的documents文件夹中
NSData *imageData = UIImagePNGRepresentation([UIImage imageWithCGImage:newImage]);
CGImageRelease(newImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [NSString stringWithFormat:@"%@/%@%d_%d.png",documentsDirectory, prefix, x, y];
[imageData writeToFile:path atomically:NO];
一些可能的选择
- 在后台执行调整大小操作,这样就不会阻塞主线程
- 缓存调整大小后的图像,这样第一次只会有延迟(这可以与在后台执行操作结合使用,这样在滚动时就不会有延迟)
- 你正在下载图片吗?如果是这样,请先下载缩略图并显示它。然后,如果选择了图像,则显示放大至全屏的缩略图,该缩略图看起来会很块状,但随后在后台下载全尺寸图像,并在加载后显示
- 如果你没有下载这些图片,但它们包含在应用程序中,那么只需包含缩略图并显示它们即可
- 在后台执行调整大小操作,这样就不会阻塞主线程
- 缓存调整大小后的图像,这样第一次只会有延迟(这可以与在后台执行操作结合使用,这样在滚动时就不会有延迟)
- 你正在下载图片吗?如果是这样,请先下载缩略图并显示它。然后,如果选择了图像,则显示放大至全屏的缩略图,该缩略图看起来会很块状,但随后在后台下载全尺寸图像,并在加载后显示
- 如果你没有下载这些图片,但它们包含在应用程序中,那么只需包含缩略图并显示它们即可
- 一些可能的选项
将imageView添加到单元格中,并将图像添加到imageView中。绘图图像通常需要时间。我认为您可以根据需要指定帧大小,尝试将图像放置到imageView中。我不会影响图像的外观
将imageView添加到单元格中,并将图像添加到imageView中取决于您拥有的图像数量,您只需将所有缩小的图像保留在属于类属性的NSMutableDictionary中即可 换言之: 在你的头上
@property (nonatomic, retain) NSMutableDictionary *sizedImages;
然后在您的cellForRowAtIndexPath中:代码
UIImage *sizedImage = [sizedImages objectForKey:[NSNumber numberWithInt:indexPath.row]];
//Or you could use the filename as the key
if (!sizedImage) {
sizedImage = [self sizeImage];(your sizing method)
[sizedImages setObject:sizedImage forKey:[NSNumber numberWithInt:indexPath.row]];
}
根据您拥有的图像数量,您可以将所有缩小的图像保存在属于类属性的NSMutableDictionary中 换言之: 在你的头上
@property (nonatomic, retain) NSMutableDictionary *sizedImages;
然后在您的cellForRowAtIndexPath中:代码
UIImage *sizedImage = [sizedImages objectForKey:[NSNumber numberWithInt:indexPath.row]];
//Or you could use the filename as the key
if (!sizedImage) {
sizedImage = [self sizeImage];(your sizing method)
[sizedImages setObject:sizedImage forKey:[NSNumber numberWithInt:indexPath.row]];
}
这两个链接非常有用 以及他的后续职位
第一个环节是一些最佳实践。第二个链接将对您更有用;将整个表格视图单元格呈现为一个图像,并使用该图像-该图像可以缓存到磁盘以释放内存,并且可以在应用程序运行之间使用。这两个链接非常有用
- (void)saveImage:(UIImage *)image withName:(NSString *)name {
[myArray addObject:name];
//save image
NSData *data = UIImageJPEGRepresentation(image, 1.0);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
[fileManager createFileAtPath:fullPath contents:data attributes:nil];
}
- (UIImage *)loadImage:(NSString *)name {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
UIImage *res = [UIImage imageWithContentsOfFile:fullPath];
return res;
}
以及他的后续职位
第一个环节是一些最佳实践。第二个链接将对您更有用;将整个表视图单元格呈现为一个图像,并使用该图像-该图像可以缓存到磁盘以释放内存,并且可以在应用程序运行之间使用
- (void)saveImage:(UIImage *)image withName:(NSString *)name {
[myArray addObject:name];
//save image
NSData *data = UIImageJPEGRepresentation(image, 1.0);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
[fileManager createFileAtPath:fullPath contents:data attributes:nil];
}
- (UIImage *)loadImage:(NSString *)name {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
UIImage *res = [UIImage imageWithContentsOfFile:fullPath];
return res;
}
最后,我得到了一个同时加载大量大尺寸图像的解决方案。首先,我使用上面的大小缩减代码缩减了图像的大小,并首次将这些大小的图像保存到documents目录,然后使用这些saveImage和loadImage函数从documents目录加载保存的图像
感谢所有的意见和建议来解决这个问题
最后,我得到了一个同时加载大量大尺寸图像的解决方案。首先,我使用上面的大小缩减代码缩减了图像的大小,并首次将这些大小的图像保存到documents目录,然后使用这些saveImage和loadImage函数从documents目录加载保存的图像
感谢您为解决此问题提供的所有意见和建议。正确的解决方案-您可以通过告诉OP从何处获取路径组件来增加此答案的亮度:D。正确的解决方案-您可以通过告诉OP从何处获取路径组件来增加此答案的亮度:D。这是一个非常好的解决方案苹果的官方例子:我认为你会在那里找到正确的方法。苹果的官方例子非常好:我认为你会在那里找到正确的方法。。。。