加载 iphone nsmutablearray 图像库的最佳方法(600+ 项)

发布于 2024-09-30 13:28:41 字数 1048 浏览 4 评论 0原文

我创建了一个具有图像库的应用程序。我已将图像导入到 Xcode 中的资源文件夹中,并且当前在应用程序加载时将它们加载到 appDelegate 中的数组中。这种情况每次都会发生。我现在担心随着库的增长,应用程序将耗尽内存。有更好的方法来加载这些吗?

结构是

Library >类别>图像列表>我

有一个名为 mainLibrary 的 NSMutableArray,然后为每个类别添加另一个 nsmutablearray。例如,

NSMutableArray *arrHome = [[NSMutableArray alloc] initWithObjects:  @"Home",                           
    [NSDictionary dictionaryWithObjectsAndKeys: [NSString stringWithFormat:@"%@/%@", resourcePath, @"alarm_clock.png"], @"image_path", @"Alarm Clock", @"title", nil],
    [NSDictionary dictionaryWithObjectsAndKeys: [NSString stringWithFormat:@"%@/%@", resourcePath, @"bathtub.png"], @"image_path",@"Bathtub", @"title", nil],

    nil];
[mainLibrary addObject: arrHome];

上面是“家庭”类别,仅包含两个图像。对于其他类别,我为每个图像设置了 NSDictionary 行 - 每个类别中最多可以有 100 个图像。

如果我将超过 50mb 的图像加载到数组中,这是否会导致应用程序变得无响应 - 或者更有可能,只是崩溃,因为它使用了每个应用程序更多的 24mb(左右)配额?

我想知道是否应该执行上述代码并在选择类别后读入数组 - 但这可能会导致用户停止运行。

有什么想法吗?

ps)我还需要考虑一下视网膜显示屏。有 600 多个图像,复制它们意味着我的二进制文件大小会猛增!

I have created an app which has a library of images. I have imported the images in to my resources folder in Xcode and am currently loading them in to an array in the appDelegate when the app loads. This is happening each time. I am now worried that as the library grows the app will run out of memory. Is there a better way of loading these?

the structure is

Library > category > image list > image

I have an NSMutableArray called mainLibrary which I then add another nsmutablearray for each category. For example

NSMutableArray *arrHome = [[NSMutableArray alloc] initWithObjects:  @"Home",                           
    [NSDictionary dictionaryWithObjectsAndKeys: [NSString stringWithFormat:@"%@/%@", resourcePath, @"alarm_clock.png"], @"image_path", @"Alarm Clock", @"title", nil],
    [NSDictionary dictionaryWithObjectsAndKeys: [NSString stringWithFormat:@"%@/%@", resourcePath, @"bathtub.png"], @"image_path",@"Bathtub", @"title", nil],

    nil];
[mainLibrary addObject: arrHome];

The above is for the "home" category and contains only two images. For other categories, I have the NSDictionary line for each image - there can be up to 100 images in each category.

If I am loading in 50mb+ of images to an array will this cause the app to become unresponsive - or more than likely, simply crash as it uses more of the 24mb (ish) allowance per app??

I am wondering whether I should do the above code and read in to the array ONCE a category has been selected - but that might then cause it to stall for the user.

Any thoughts?!

ps) I also need to have a think about Retina display. With 600+ images, duplicating them would mean my binary size would rocket!!

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

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

发布评论

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

评论(3

自在安然 2024-10-07 13:28:41

不久前,我对我正在开发的一个应用程序进行了一些测试。连续加载 5000 张图像:

[UIImage imageWithData:...] // 44.8 秒

[UIImage imageWithContentsOfFile:...] // 52.3 秒

[[UIImage alloc] initWithContentsOfFile:...] // 351.8 秒

[UIImage imageNamed:...] // 由于缓存而挂起

使用文件路径显然是正确的方法。显然,内存管理将是您的情况的关键。

A while ago I ran some tests for an app I was working on. Loading 5000 images in succession:

[UIImage imageWithData:...] // 44.8 seconds

[UIImage imageWithContentsOfFile:...] // 52.3 seconds

[[UIImage alloc] initWithContentsOfFile:…] // 351.8 seconds

[UIImage imageNamed:...] // hung due to caching

Using the paths to the files is obviously the way to go. Obviously memory management will be key in your case.

赏烟花じ飞满天 2024-10-07 13:28:41

使用字符串组成路径字典/数组,然后根据需要加载/卸载图像(延迟加载),如果您需要这样的结构在程序中进行交互。

600 张 HQ 图像将超出内存限制,而且这是一种浪费,因为用户在给定时间会看到多少张这些图像?资源非常有限,并且高清图像需要大量空间。

如果您必须显示一堆缩略图,那么这些缩略图可能会很容易地缩小和导出,以便您可以在合理的时间内显示所有图像,同时使用合理的内存量(同样,只需在可能的情况下延迟加载并释放不太容易查看时的图像)。

compose your path dictionaries/arrays using strings, then load/unload images as needed (load lazily), if you need such a structure to interface with in your program.

600 HQ images will exceed the memory limits, and it's wasteful because the user will see how many of these images at a given time? resources are quite limited, and hq images require a lot of space.

if you have to display a bunch of thumbnails, those may be shrunk and exported easily enough so you can display all the images in a reasonable amount of time, while using a rational amount of memory (again, just load lazily where possible and release the image when not very easily viewed).

一花一树开 2024-10-07 13:28:41

为什么不直接使用 [UIImage imageNamed:IMAGE_NAME] ?它内部有很好的内存管理。

Why not use [UIImage imageNamed:IMAGE_NAME] directly ? It has a good memory management inside.

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