简体   繁体   English

如何从在线目录加载UIImage数组中的图像并将其存储在App中

[英]How to load images in UIImage array from an online directory and store them in the App

I want to load multiple images when my app starts from a website (ie all images in http://hello.com/images/ which are named 1.png, 2.png, 3.png..) , so that the images can be used anywhere in the program without needing to reload them every time I want to access them. 我想从网站启动应用程序时加载多个图像(即http://hello.com/images/中的所有图像,分别命名为1.png,2.png,3.png ..),以便这些图像可以在程序中的任何位置使用,而无需每次我要访问它们时都重新加载它们。

Can I simply create a class that holds a static NSArray and fill it at the beginning, to then create an instance of this class whenever I need the images or is there a better way to do it? 我可以简单地创建一个包含静态NSArray的类并在开始时填充它,然后在需要图像时创建该类的实例,或者有更好的方法吗?

Right now, I am loading the images with the following code: 现在,我正在使用以下代码加载图像:

UIImage *image =[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://hello.com/images/%@.png,item]]]];

I want to make the app as efficient as possible, so I am concerned about the creation of multiple objects making it very demanding. 我想使该应用程序尽可能高效,因此我担心创建多个对象使其要求很高。

Thanks 谢谢

You can try downloading the images asynchronously in a separate thread when the application starts and use it later. 您可以尝试在应用程序启动时在单独的线程中异步下载图像,并在以后使用它。

Here is the SO question and answer where the poster uses a custom class to download the images in the background asynchronously. 是SO的问答,发帖者使用自定义类在后台异步下载图像。

Try this for efficient download of images and UI also will not be blocked. 尝试此操作可有效下载图像,UI也不会被阻止。

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

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