简体   繁体   English

IOS - 处理大量图像的最佳实践(性能+磁盘大小)

[英]IOS - Best practice to handle large number of images (Performance + Size on disk)

My app UIKit stores something like 100 (100x100) jpeg files, that suppose to be used as "pattern image". 我的应用程序UIKit存储类似100 (100x100) jpeg文件,假设用作“模式图像”。 the average size for each image is sometimes like 20~40 kb . 每张图像的平均大小有时像20~40 kb

I'm cocos2d-x developer as well. 我也是cocos2d-x开发人员。 In cocos environment i'm using plist to "bound" each image and than cut it via plist . 在cocos环境中,我使用plist “绑定”每个图像,而不是通过plist切割它。 This is great performance and size saver, but as far as I know its not possible on UIKit . 这是伟大的性能和尺寸保护,但据我所知,它在UIKit不可能的。

So my question is this, Is there a better way to store/pull this images to improve disk capacity and increase performance, other than drag and drop the files into XCode and use it as usual? 所以我的问题是, 有没有更好的方法来存储/拉动这些图像以提高磁盘容量和提高性能,除了将文件拖放到XCode并像往常一样使用它?

[any number] of images is not really a problem on iOS since there are advanced caching systems in place that take care of reuse of the images. [任意数量]的图像在iOS上并不是真正的问题,因为有适当的高级缓存系统来处理图像的重用。 Also the rendering system of iOS is quite great so you don't have to worry about it. iOS的渲染系统也非常棒,所以你不必担心它。

While it is certainly possible to program system for sprites, I would not advice you to do it. 虽然可以为sprite编程系统,但我不建议你这样做。 This method is primarily used in Web Development (because each image has to be served with new request *note no longer true with HTTP/2 ) and also obviously for game development (because the less bind texture calls you have, the better). 这种方法主要用于Web开发(因为每个图像必须提供新的请求*注意不再适用于HTTP / 2 ),并且显然也适用于游戏开发(因为你拥有的绑定纹理越少越好)。

There is one more good example for why not to use sprites - if you are developing Watch application and you want to do animation, you do it through flipboard style images (sequence of images named 1.png - 100.png), and not by using large atlas of images. 还有一个很好的例子说明为什么不使用精灵 - 如果你正在开发Watch应用程序而你想要做动画,你可以通过flipboard样式图像(图像序列命名为1.png - 100.png),而不是使用大型图像图集。 While it would be guessing as why did they decide to do it that way (my guess would be because of how good it works internally + throughput of the bluetooth), it is obvious that it is also preferred choice of Apple and so we should follow. 虽然它会猜测为什么他们决定这样做(我的猜测是因为它内部工作有多好+蓝牙的吞吐量),很明显它也是Apple的首选,所以我们应该关注。

For iOS, there are some pitfalls that you should know: 对于iOS,您应该知道一些陷阱:

  • Any image loaded from the Web should not be loaded on main thread, the same really goes with every image that is not present at the load of the screen (images in UITableViewCell are great example, because you can see mini-freezes if the images are large as you are scrolling through) 从Web加载的任何图像都不应该加载到主线程上,对于屏幕加载时不存在的每个图像都是如此(UITableViewCell中的图像是很好的例子,因为如果图像是你可以看到迷你冻结当你滚动时很大)
  • If you have many layers, images with alpha channel != 1 decrease performance heavily (but are often unavoidable) 如果你有很多图层,那么带alpha通道的图片!= 1会大幅降低性能(但通常是不可避免的)
  • Background image created with UIColor.colorWithPatternImage() should be used with caution, as this method is considered problematic ( details here ) 使用UIColor.colorWithPatternImage()创建的背景图像应谨慎使用,因为此方法被认为是有问题的( 详情请参见此处

Now as for asynchronous loading of the images, I suggest you take a look at following libraries: 现在至于图像的异步加载,我建议你看看下面的库:

Both of them are great so it is really matter of preference (I like Haneke better), but they allow you to download images on different threads, be it from Web or from your bundle. 它们都很棒,所以它确实是偏好的问题(我更喜欢Haneke)​​,但它们允许您在不同的线程上下载图像,无论是来自Web还是来自您的捆绑。 They also have extensions for UIImageView which allows you to use 1-line function to load all images easily. 它们还具有UIImageView扩展,允许您使用1行功能轻松加载所有图像。

Hope it helps! 希望能帮助到你!

There will be in iOS 9 if that counts ;) It's called On Demand Resources and lets you store content with Apple and download the content when it's needed for a level, etc. It'll be great for game apps and is what they used as an example at WWDC. 如果重要的话,将在iOS 9中使用;)它被称为On Demand Resources并允许您存储Apple的内容并在需要时下载内容等等。它对游戏应用程序来说非常棒,并且它们用作WWDC的一个例子。

Check it out here: https://developer.apple.com/library/prerelease/ios/documentation/FileManagement/Conceptual/On_Demand_Resources_Guide/ 请在此处查看: https//developer.apple.com/library/prerelease/ios/documentation/FileManagement/Conceptual/On_Demand_Resources_Guide/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM