Search code examples
iosnsdatamp4cloudkitckasset

How do I store mp4 file as Data or NSData object in iOS?


How would I store media of type mp4 as an object of Data or NSData in iOS? I need to do this as a step toward saving the mp4 file as a CKAsset in CloudKit.

I would like to do what this post on stackoverflow does with an image object, except I want to do it with an mp4 file.


Solution

  • I've not tried this, but maybe the Data initializer that takes a URL would do the trick:

    init(contentsOf:options:)

    E.g.:

    let url = Bundle.main.url(forResource:"myFile", withExtension:"mp4")
    let data = Data(contentsOf: url)
    

    See the Apple docs