Replacing images in the bundle at run time

ぃ、小莉子 提交于 2019-12-19 10:25:18

问题


Can I replace images in bundle at run time? Basically, I have some images part of my bundle and also I will have them from the server if there is any change in the image. Can I place them in the bundle at run time so that I need not to change my code to pick the image?


回答1:


You cannot change the contents of an Application bundle.

It is however possible to store the images in the Application's documents folder. Then you'll be able to overwrite those images in case they need to be changed.

There are two ways to achieve this:

  1. At launch, check whether the image files exist in the documents directory. If not, copy the bundle images to the document folder.
  2. Every time you load an image, check if it exists in the documents directory. If it does, load it, otherwise load the one from the bundle. This avoids copying the data thus leading to less disk usage, however it's more work to implement.



回答2:


You cannot change the bundle on runtime. This will prevent the app from running, as the profile won't be valid anymore. You will find tons of code how to deal with the sandbox folders provided by iOS on stackoverflow.

edit
Useful questions / answers

  • copying folder from bundle to documents
  • Downloading image into bundle


来源:https://stackoverflow.com/questions/4764591/replacing-images-in-the-bundle-at-run-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!