I use Core Data to store the URL path of the image file in my document, how to backup both of them to mac/PC, I copy the sq开发者_JAVA百科lite file and the image out through iTune, and when I copy them back to iPhone, the sqlite seem to be ok, but the image didn't show up. I think the path has changed.
what can I do?
I have done something similar. Two things were key to making this architecture work.
- The image files must be stored in a consistent location relative to the Core Data store file.
- The URL/path stored in Core Data for the image is a relative one, not absolute. The image path is made relative to the Core Data store file.
Relative Location on Filesystem
I stored all images in a subdirectory named "Images" that lived in the same directory as the Core Data store file. E.g.
parent directory/ --> MyCoreDataStore.sql --> Images/ -----> SomeImage1.png -----> SomeImage2.png
Essentially, I treat the application's data as a file package: a directory containing multiple related files that all together comprise the application data or data document.
Relative image URLs
URLs (or paths) to images must be constructed at runtime. The Core Data store holds a relative path/URL to each image's location. The location is relative to the location of the Core Data store file. Your app always knows the full path/URL to the Core Data store.
I build the full path to an image location when needed using the parent directory of the Core Data store file and the relative image path held in the Core Data db.
精彩评论