如何将图片加载到 iPhone 系统缓存中?

发布于 2024-09-08 09:30:24 字数 753 浏览 1 评论 0原文

我有一个 tableView,里面有一些大图像。我正在努力改善非常不稳定的滚动性能。如果我使用 ImageNamed 加载图像,一开始滚动会很卡顿,但查看图像后,滚动会很平滑。我知道 ImageNamed 将图像添加到系统缓存中,所以我的问题是:是否可以在查看图像之前将图像预加载到系统缓存中?

我尝试将以下代码添加到我的 viewDidLoad 方法中:

for (int i = 0; i < appDelegate.detailSectionsDelegateDict.count; i++) {
         NSString *imageString = [NSString stringWithFormat:@"%@",[[appDelegate.detailSectionsDelegateDict objectAtIndex:i] objectForKey:@"MainTrackImage"]];
        NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
        UIImage* theImage;
        theImage = [UIImage imageNamed:imageString];
         [imageCacheArray insertObject:theImage atIndex:i];
}

然后,我在 CellForRowAtIndexPath 方法中从 imageCacheArray 中绘制正确的图像。但结果仍然是滚动不稳定。

谢谢!

I have a tableView with some large images in it. I'm struggling to improve the very jerky scrolling performance. If I use ImageNamed to load the images, scrolling is jerky at first, but after the images are viewed, scrolling is smooth. I know ImageNamed adds the images into the system cache, so my question is: is it possible to pre-load the images into the system cache before they are viewed?

I've tried by adding the following code to my viewDidLoad method:

for (int i = 0; i < appDelegate.detailSectionsDelegateDict.count; i++) {
         NSString *imageString = [NSString stringWithFormat:@"%@",[[appDelegate.detailSectionsDelegateDict objectAtIndex:i] objectForKey:@"MainTrackImage"]];
        NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
        UIImage* theImage;
        theImage = [UIImage imageNamed:imageString];
         [imageCacheArray insertObject:theImage atIndex:i];
}

I then draw the correct image from the imageCacheArray in my CellForRowAtIndexPath method. But the result is still jerky scrolling.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单身情人 2024-09-15 09:30:24

让带有图像(尤其是大图像)的表格视图平滑滚动并不像您想象的那么简单。使用 [UIImage imageNamed:] 加载一堆图像将很快导致 springboard 杀死您的应用程序,因为它开始超出内存容量。看看核心动画今年 WWDC 的会议视频,具体看第 425 次会议,“实践中的核心动画,第 2 部分” 他们涵盖了这个确切的主题,而且做得非常好。使用开发者账号登录也可以获得相关源码。

Getting a table view with images (especially large ones) to scroll smoothly is not as trivial as you might think. Loading up a bunch of images with [UIImage imageNamed:] will very quickly cause springboard to kill your app as it starts to exceed memory capacity. Take a look at the Core Animation session videos from this year's WWDC, specifically look at session 425, "Core Animation in Practice, Part 2" They cover this exact topic and it's very well done. You can also get the relevant source code if you sign in with your developer account.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文