照片库怎么显示这么多图片呢?

发布于 2024-08-16 05:48:02 字数 198 浏览 4 评论 0原文

扩展一下这个主题,......而不崩溃?我从 UIImageViews 的照片库中加载了大约 15 张全尺寸图像,并收到内存不足警告,最终导致应用程序崩溃。这些图像的大小从 250KB 到 400KB 左右。 UIImageViews 设置为宽高比。

照片库似乎加载较小文件大小的图像版本,而不仅仅是调整其比例。有谁知道它如何能够显示这么多而不会因为内存使用而崩溃?

To extend on the subject, ...without crashing? I load about 15 full size images from the photo library in UIImageViews and get out of memory warnings and eventually an app crash. These images range from around 250KB to 400KB. The UIImageViews are set to aspect fit.

The photo library seems to load smaller file size versions of images, rather than just resizing their proportions. Does anyone know how it is able to display so many without crashing because of memory usage?

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

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

发布评论

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

评论(2

时常饿 2024-08-23 05:48:02

正如你所说,你会想要调整图像的大小。调整图像大小并不像您想象的那么明显,但这并不难:

// h = new height; w = new width; image = UIImage
CGSize newSize;
newSize.height = h;
newSize.width = w;
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0, 0, w, h)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

当然,您需要确保释放旧图像,以便可以回收内存。最好的方法可能是使用 +imageWithContentsOfFile: ,因为它明确不缓存。

As you say, you'll want to resize the images. Resizing an image isn't quite as obvious as you'd think, but it's not hard:

// h = new height; w = new width; image = UIImage
CGSize newSize;
newSize.height = h;
newSize.width = w;
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0, 0, w, h)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Of course you'll want to make sure you release your old images so you can reclaim the memory. The best approach is probably to use +imageWithContentsOfFile: since it explicitly doesn't cache.

絕版丫頭 2024-08-23 05:48:02

照片库可能执行许多内存优化:

  1. 加载照片的缩略图,作为图库的一部分显示
  2. 仅加载现在显示的照片的缩略图(例如不是下一页中的缩略图)
  3. 仅加载一次大图像它实际上是需要的。
  4. ...

为了让您的生活更轻松,我建议使用 Facebook Three20 库,其中包括照片查看器:

TTPhotoViewController 模拟 Apple 的照片应用程序其轻弹和捏捏的乐趣。您可以提供自己的“照片源”,其工作方式与 UITableView 使用的数据源类似。与苹果的照片应用程序不同,它不仅限于本地存储的照片。您可以从网络加载照片,并且可以增量加载长照片列表。

该项目有一个很好的示例代码,因此您可以从那里开始。顺便说一句,这与 Facebook iPhone 应用程序中使用的视图相同。它的外观如下:

alt text

The photo library performs many memory optimizations, propably:

  1. loading a thumbnail of the photo, to display as part of the gallery
  2. only loading the thumbnails for the photos on display now (not the ones in next page for example)
  3. only loading the large images once it's actually needed.
  4. ...

To make your life easier, I recommend using the Facebook three20 library, which includes a photo viewer:

TTPhotoViewController emulates Apple's Photos app with all of its flick n' pinch delight. You can supply your own "photo sources", which works similiarly to the data sources used by UITableView. Unlike Apple's Photos app, it isn't limited to photos stored locally. Your photos can be loaded from the network, and long lists of photos can be loaded incrementally.

The project has a nice sample code, so you can start from there. By the way, this is the same view used in the Facebook iPhone app. Here is how it looks like:

alt text

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