从本地文件系统检索视频数据

时间:2015-10-14 10:12:20

标签: ios swift

我正在创建一个简单的应用,将录制的视频上传到网络API。在应用程序中录制视频后,我可以在应用程序中成功播放视频,但我很难访问该视频进行上传。

视频位于assets-library://asset/asset.MOV?id=3AFCEC9B-17DE-4D75-9B87-0AD50BAB9BFF&ext=MOV,可以使用MPMoviePlayerController(contentURL: url)加载,因此我知道它存在于此处。

我尝试了以下几种方法但没有成功:

方法1

let url = NSURL(fileURLWithPath: thisNote.url!)
        println("This url = \(thisNote.url)")
let videoData = NSData(contentsOfURL: url!, options: nil, error: &e)

错误= The operation couldn’t be completed. No such file or directory

方法2

let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let docsDir = dirPaths[0] as! String
let videoFilePath = docsDir.stringByAppendingPathComponent(Constants.Directories.appFolder)
let filePath = NSBundle.pathForResource(thisNote.fileName, ofType: "video/quicktime", inDirectory: Constants.Directories.appFolder)

filepath = nil

方法3

var video:NSData = NSData()
ALAssetsLibrary().assetForURL(url, resultBlock: { (asset : ALAsset!) -> Void in
    if let rep : ALAssetRepresentation = asset.defaultRepresentation(){

        var error: NSError?
        let length = Int(rep.size())
        let from = Int64(0)
        let data = NSMutableData(length: length)!
        let numRead = rep.getBytes(UnsafeMutablePointer(data.mutableBytes), fromOffset: from, length: length, error: &error)

        video = data

        self.uploadNote(video, note: thisNote)

    }
}){ (error : NSError!) -> Void in
     println("Asset library error: \(error)")
}

未到达结果块,也未打印错误。

对此有任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

方法三最接近最终解决方案。我最后通过资产库枚举来找到合适的视频文件。虽然我确信这不是获取视频的最有效方式,但它确实有效。这是我使用的代码,我希望它可以帮助某人......

    let url = NSURL(string: thisNote.url!)
    var video:NSData = NSData()
    AssetLibraryHelper.sharedInstance.assetForURL(url, resultBlock: { (foundAsset : ALAsset!) -> Void in
        var asset:ALAsset?

        if foundAsset == nil {
            // Workaround: Enumerate over the asset library to find matching URL
            AssetLibraryHelper.sharedInstance.enumerateGroupsWithTypes(
                ALAssetsGroupType(ALAssetsGroupPhotoStream),
                usingBlock: { (group: ALAssetsGroup?, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
                    if group != nil {
                        group!.enumerateAssetsWithOptions(NSEnumerationOptions.Reverse, usingBlock: {(enumeratedAsset: ALAsset!, index: Int, stopGroup: UnsafeMutablePointer<ObjCBool>) -> Void in

                            if enumeratedAsset != nil {
                                    asset = enumeratedAsset
                                    stop.initialize(true)
                                    stopGroup.initialize(true)
                            }

                        })
                    }
                }, failureBlock: { (error: NSError!) -> Void in
                print("Error enumerating assets \(error)")
            })
        }
        else {
            asset = foundAsset
        }

        print("Asset is \(asset)")

        if let rep : ALAssetRepresentation = asset!.defaultRepresentation(){

            let image = UIImage(CGImage: rep.fullResolutionImage().takeUnretainedValue())
            let imageData = UIImageJPEGRepresentation(image, 0.7)

            var error: NSError?
            let length = Int(rep.size())
            let from = Int64(0)
            let data = NSMutableData(length: length)!
            let _ = rep.getBytes(UnsafeMutablePointer(data.mutableBytes), fromOffset: from, length: length, error: &error)

            video = data

            self.uploadNote(video, note: thisNote, imageFile: imageData!)

        }
        }){ (error : NSError!) -> Void in
            print("Asset library error: \(error)")
        }