目录加载时间

时间:2012-05-23 01:00:22

标签: objective-c ios image directory

我的应用程序的一个功能是允许用户保存图像。稍后将这些图像显示在UITableView的详细视图中以及UITableView的单元格中。但是,当返回到显示单元格的页面时,它需要相当长的时间才能加载。我相信这是因为图像保存到文档目录中,每次加载页面时都会从目录重新加载。每次应用程序启动时,我是否应该将图像存储到内存中,或者在我的理论中将其用完。我也尝试使用以下代码压缩图像,但这并没有提高性能。

       [[GTImageStore sharedStore] setImage:currentImage forKey:currentImageKey];

        NSString *jpgName = [[NSString alloc] initWithFormat:@"Documents/%@.jpg", currentImageKey];
        NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:jpgName];

        float scale;
        if (currentImage.size.width < currentImage.size.height)
        {
            scale = 100/currentImage.size.width;
        }
        else if (currentImage.size.width > currentImage.size.height)
        {
            scale = 100/currentImage.size.height;
        }
        else
        {
            scale = 100/currentImage.size.width;
        }

        UIImage *image = [[UIImage alloc] initWithCGImage:currentImage.CGImage scale:scale orientation:[currentImage imageOrientation]];

        [UIImageJPEGRepresentation(image, 0.5) writeToFile:jpgPath atomically:YES];

非常感谢任何帮助!谢谢!

添加单元格的代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     UINib * giftCell = [UINib nibWithNibName:@“GiftCell”bundle:nil];     [tableView registerNib:giftCell forCellReuseIdentifier:@“UIGiftsTableCell”];

cell = [tableView dequeueReusableCellWithIdentifier:@"UIGiftsTableCell"];
if (!cell) {
    cell = [[GTGiftCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UIGiftsTableCell"];
}

// Determine gift at index path
GTGift *gift = (GTGift *)[[[GTGiftStore sharedStore] allGifts] objectAtIndex:[indexPath row]];

[[cell textLabel] setText:[gift name]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
NSString *subText = [[NSString alloc] initWithFormat:@"Purchased on %@", [formatter stringFromDate:[gift datePurchased]]];
[[cell subTextLabel] setText:subText];

[[cell detailTopTextLabel] setText:[[gift givingTo] name]];
[[cell detailMiddleTextLabel] setText:[[gift reasonFor] name]];
[[cell detailBottomTextLabel] setText:[[gift boughtFrom] name]];

NSString *jpgName = [[NSString alloc] initWithFormat:@"Documents/%@.jpg", [gift imageKey]];
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:jpgName];
UIImage *imageToLoad = [[UIImage alloc] initWithContentsOfFile:jpgPath];
[[[cell image] layer] setBorderColor:[[UIColor lightGrayColor] CGColor]];
[[[cell image] layer] setBorderWidth:1];
[[cell image] setClipsToBounds:YES];
[[cell image] setContentMode:UIViewContentModeScaleAspectFill];
[[cell image] setImage:imageToLoad];

return cell;

}

我使用带有名为GTGiftCell的viewController的xib文件创建了一个自定义单元格。

1 个答案:

答案 0 :(得分:0)

您可以采取两种方法

  1. 如果您的应用程序需要在更改时加载图像,那么您可以按原样加载图像,但是如果图像需要更新和更改,则应该阅读apple Concurrency Programming Guide以提高性能。用户刷新视图
  2. 在加载应用程序时加载图像,但再次提高性能请阅读Concurrency Programming Guide