Get NSData from assets-library URL

后端 未结 2 1357
广开言路
广开言路 2021-01-06 18:42

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         


        
2条回答
  •  花落未央
    2021-01-06 18:56

    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
            }
        }
    

提交回复
热议问题