I want retrieve non jpg/png files from the Asset Catalog in Xcode 7. I saw it was possible now to add any kind of data into to Asset Catalog.
So the way I was doing it was working before moving gif/video from Xcode project into the Assets Catalog
private struct WalkthroughVideo {
static let name = "tokyo"
static let type = "mp4"
}
private var moviePlayer : AVPlayerViewController? = {
guard let path = NSBundle.mainBundle().pathForResource(WalkthroughVideo.name, ofType: WalkthroughVideo.type) else {
return nil
}
let url = NSURL.fileURLWithPath(path)
let playerViewController = AVPlayerViewController()
playerViewController.player = AVPlayer(URL: url)
return playerViewController
}()
So now path is nil. It can't find the file. Is there a new way to retrieve dataset ?
Obviously
pathForResource
is not going to work, because the resource is not in your app bundle any more - it's in your asset catalog. In effect, it no longer has any "path".You have to use the new NSDataAsset class, which can fetch arbitrary data by name out of the asset catalog. To simulate your tokyo.mp4 example, I used an AIFF sound file, stored in my asset catalog as a data set called theme (in the Universal slot). Here's my complete test code (in my root view controller):
Lo and behold, the sound played! Of course in real life I would have added error handling on that
try
.