2011-06-30 57 views
0

我使用UITableView在每个表视图单元格中显示2到3个图像作为缩略图。我在桌面视图中显示的图像尺寸很大。如果我直接使用这些图像而不减小任何尺寸,那么如果我在TableView和其他视图控制器之间来回移动,tableview可能会丢失一些图像。由于处理UITableView滚动不平滑

所以,我想到缩小图像尺寸的缩小代码。下面给出的代码很好,前一个问题已修复。但是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; 

回答

0
- (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; 
    } 

最后,我得到了一个解决方案来同时加载大量图像。首先,我使用上面的缩小代码缩小了图像的大小,并将这些大小的图像首次保存到文档目录,然后使用这些saveImage和loadImage函数从文档目录加载保存的图像。

感谢所有的意见和建议,以解决这个问题。

3

在减少,你可以得到的newImage存储为您的应用程序的文档文件夹中的文件图像的顶部。

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]; 
+0

正确的解决方案 - 您可以通过告诉OP从何处获取路径组件来增加此答案的亮度。 – Till

+0

@Till - 感谢提示,我更新了代码示例。 – werner

0

有几个可能的选项

  • 所以它不撑起主线程在后台执行调整大小。
  • 这样做后缓存大小调整的图像,所以只有第一次延迟(这可以在背景中使用,所以没有延迟滚动)
  • 你下载的图像?如果是这样,请首先下载缩略图并显示该缩略图。然后,如果选择了图像,则显示缩放至缩略图的缩略图,然后在后台下载全尺寸图像,并在加载后显示。
  • 如果你不下载图像,但它们包含在应用程序中,只包括缩略图,以及和显示他们,而不是
0

绘制图像通常需要再寄一次想你可以尝试把图像在imageView中通过给出所需的帧尺寸,在图像外观上没有区别。

的imageviews添加到细胞,并添加图片到ImageView的

2

根据您拥有的图片数量,您可以将所有尺寸缩小的图片放在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]]; 
}