Iphone application: Managing downloaded images
Hi I am currently trying to building a reader app for the iphone, a bit like NYTimes iphone app or Time magazine or USA today iphone app. Basically I'll be parsing a feed containing articles, saving the data locally in 开发者_如何转开发a database and displaying stuff and all that.
Now I am just starting off with Iphone app development, I would just like to know what is the best way to manage Images in these sort of apps. Id like to save the images for each article locally. Which is the preferred method for doing so, saving it into a database or saving it to the file system. There are a lot of images and managing them would be easier if i store it in a blob field in the database, but that would affect performance dramatically I guess. Saving it into the filesystem, in the application documents directory would give me better performance but I am wondering how would I manage a large amount of file (Saving it, keeping track of an Image of a particular article, deleting files etc)
From the Core Data Programming Guide:
It is better, however, if you are able to store BLOBs as resources on the filesystem, and to maintain links (such as URLs or paths) to those resources. You can then load a BLOB as and when necessary.
The idea is to load images lazily, only when they need to appear on screen. This is very important on the device.
Consider how many managed objects will be in memory at once. When the objects are instantiated, if you have the image saved as part of the object, it will be loaded into memory as well and will remain in memory until the object is turned into a fault.
As for the second part of your question on how to ensure that files are removed when managed objects are deleted, register for NSManagedObjectContextObjectsDidChangeNotification
and NSManagedObjectContextDidSaveNotification
and clean up the files that you can obtain from the objects in NSDeletedObjectsKey
and NSUpdatedObjectsKey
.
Core Data is probably the best solution for you. Dealing with the FS(File System) would be a bit messy.
Search Apple's docs for Core Data and you'll get all the info you need.
NYTimes apps store images on the file system: cache folder.
For large number of images, the database can be messier than the file system. Article images are also considered lazy, replaceable data. Storing a large number of them in the DB is irresponsible because you're sucking down permanent disk space on the device, user's computer (backups) and possibly iCloud.
精彩评论