我的应用程序的功能之一是允许用户保存图像。这些图像稍后会在 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 文件创建了一个自定义单元格。