从ALAsset获取视频 [英] Getting video from ALAsset

查看:626
本文介绍了从ALAsset获取视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用iOS 4中提供的新资产库框架,我看到我可以使用UIImagePickerControllerReferenceURL获取给定视频的网址。返回的网址格式如下:

Using the new asset library framework available in iOS 4 i see that I can get the url for a given video using the UIImagePickerControllerReferenceURL. The url returned is in the following format:

assets-library://asset/asset.M4V?id=1000000004&ext=M4V

我正在尝试将此视频上传到网站,以便快速证明我的概念尝试以下

I am trying to upload this video to a website so as a quick proof of concept I am trying the following

NSData *data = [NSData dataWithContentsOfURL:videourl];
[data writeToFile:tmpfile atomically:NO];

在这种情况下,数据永远不会被初始化。有没有人设法通过新资产库直接访问网址?感谢您的帮助。

Data is never initialized in this case. Has anyone managed to access the url directly via the new assets library? Thanks for your help.

推荐答案

这是一个干净利落的快速解决方案,可以将视频作为NSData。
它使用Photos框架,因为从iOS9开始不推荐使用ALAssetLibrary:

Here is a clean swift solution to get videos as NSData. It uses the Photos framework as ALAssetLibrary is deprecated as of iOS9:


重要

IMPORTANT

从iOS 9.0开始,不推荐使用Assets Library框架。相反,请使用Photos框架,在iOS 8.0及更高版本中,它可以为用户的照片库提供更多功能和更好的性能。有关详细信息,请参阅照片框架参考。

The Assets Library framework is deprecated as of iOS 9.0. Instead, use the Photos framework instead, which in iOS 8.0 and later provides more features and better performance for working with a user’s photo library. For more information, see Photos Framework Reference.



import Photos

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    self.dismissViewControllerAnimated(true, completion: nil)

    if let referenceURL = info[UIImagePickerControllerReferenceURL] as? NSURL {
        let fetchResult = PHAsset.fetchAssetsWithALAssetURLs([referenceURL], options: nil)
        if let phAsset = fetchResult.firstObject as? PHAsset {
            PHImageManager.defaultManager().requestAVAssetForVideo(phAsset, options: PHVideoRequestOptions(), resultHandler: { (asset, audioMix, info) -> Void in
                if let asset = asset as? AVURLAsset {
                    let videoData = NSData(contentsOfURL: asset.URL)

                    // optionally, write the video to the temp directory
                    let videoPath = NSTemporaryDirectory() + "tmpMovie.MOV"
                    let videoURL = NSURL(fileURLWithPath: videoPath)
                    let writeResult = videoData?.writeToURL(videoURL, atomically: true)

                    if let writeResult = writeResult where writeResult {
                        print("success")
                    }
                    else {
                        print("failure")
                    }
                }
            })
        }
    }
}

这篇关于从ALAsset获取视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆