I\'m trying to get NSData from mp4 file from device library.
This link look like this:
assets-library://asset/asset.mp4?id=32515720-939A-456F-958F-0B
Swift 3: 1. Request Asset from Photos Library 2. Request Data from Asset 3. Return block
func getDataFromAssetAtUrl( assetUrl: URL, success: @escaping (_ data: NSData) -> ()){
let fetchResult = PHAsset.fetchAssets(withALAssetURLs: [assetUrl], options: nil)
if let phAsset = fetchResult.firstObject {
PHImageManager.default().requestImageData(for: phAsset, options: nil) {
(imageData, dataURI, orientation, info) -> Void in
if let imageDataExists = imageData {
success(imageDataExists as NSData)
}
}
}
}
example use:
import Photos //this on top of your file
open func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
dismiss(animated: true, completion: nil)
let assetUrl = assetDataFromMediaInfo(info: info)!
getDataFromAssetAtUrl(assetUrl: assetUrl) { (dataOfAsset) in
//TODO do something with NSData
}
}