Reputation: 595
I am currently writing an iOS app in which my intent is to save data objects associated with the app in a DB created in Core Data. I have successfully created the DB, and plan to synchronize the data objects among different devices logged in to the same iCloud-account through iCloud. In the middle of all this, I would also like media files to be associated with the distinct data objects. However, I do not wish to save the binary data constituting a media file directly to the DB. Thus I need some kind of way to preserve the reference to the correct media file in the DB. My immediate thought was to place every media file in a specific folder, turn on iCloud sync for that folder, and save the media files' filenames to the DB. However, I am not able to retrieve the file path of any media files.
Below is a code snippet from the app. When that code is run, an assetCollection as well as a photosAsset with an index exists, and is displaying an image in the view controller. Despite this, none of the if-sentences prints any output (all of the references are nil).
if let a: String = self.photosAsset[self.index].bundleIdentifier {
println(a)
}
if let a = self.photosAsset[self.index].bundleURL {
println(a)
}
if let a: String = self.photosAsset[self.index].resourcePath {
println(a)
}
if let a = self.photosAsset[self.index].resourceURL {
println(a)
}
Any thoughts on how to retrieve the file path of an image file that is being displayed in this way? Any solutions on how to retrieve file paths in general that could be applicable to this problem?
Any thoughts on my approach in general would also be greatly appreciated.
Upvotes: 2
Views: 2200
Reputation: 7219
I think that if you are storing the photos as xcassets, then you may not be able to access a path for them, as this type can sometimes be compressed (so it seems Apple don't allow it) e.g. see the answer posted for Access Asset Catalog pathForResource
I would create a custom directory structure to store the photos, rather than store them in the bundle, e.g. in /Documents/
This would allow you to use classes such as NSFileManager to find all photos of certain types etc and load them when need be.
For information about where Apple recommends you store things see https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
Upvotes: 1