程序员必知:导出相册视频

简介: 程序员必知:导出相册视频

慢动作视频, 特殊处理

1, exportSession

let option = PHVideoRequestOptions()

option.isNetworkAccessAllowed = true

option.deliveryMode = .highQualityFormat

PHCachingImageManager.default().requestExportSession(forVideo: asset, options: option, exportPreset: AVAssetExportPresetHighestQuality) { (exportSession, info) in

guard let exportSession = exportSession else {

single(.error(KimPhotoHelperError.sourceNotFound))

return

}

exportSession.outputURL = videoPath

exportSession.outputFileType = .mp4

exportSession.exportAsynchronously {

switch exportSession.status {

case .completed:

single(.success((videoPath.relativePath, size)))

case .failed:

print(exportSession.error)

single(.error(KimPhotoHelperError.sourceNotFound))

default:

break

}

}

}//代码效果参考:http://www.ezhiqi.com/zx/art_6832.html

2, PHAssetResourceManager

let resourceManager = PHAssetResourceManager.default()

let option = PHAssetResourceRequestOptions()

option.isNetworkAccessAllowed = true

resourceManager.writeData(for: assetRescource, toFile: videoPath, options: option) { (error) in

guard let error = error else {

//single(.error(e))

exportSession(asset)

return

}

single(.success((videoPath.relativePath, size)))

}

3, 先 requestDatqa -> 再 writeData

let resourceManager = PHAssetResourceManager.default()

let option = PHAssetResourceRequestOptions()

option.isNetworkAccessAllowed = true

var videoData = Data()

resourceManager.requestData(for: assetRescource, options: option, dataReceivedHandler: { (data) in

videoData.append(data)

}) { (error) in

if let error = error {

print(error)

exportSession(asset)

// single(.error(KimPhotoHelperError.sourceNotFound))

} else {

do {

try videoData.write(to: videoPath)

} catch {

}

single(.success((videoPath.relativePath, size)))

}

}

4, 导出慢动作视频 AVCompostion 类型

func ConvertAvcompositionToAvasset(avComp: AVComposition, completion:@escaping (_ avasset: AVAsset) -> Void){

let exporter = AVAssetExportSession(asset: avComp, presetName: AVAssetExportPresetHighestQuality)

let randNum:Int = Int(arc4random())

//Generating Export Path

let exportPath: NSString = NSTemporaryDirectory().appendingFormat("(randNum)"+"video.mov") as NSString

let exportUrl: NSURL = NSURL.fileURL(withPath: exportPath as String) as NSURL

//SettingUp Export Path as URL

exporter?.outputURL = exportUrl as URL

exporter?.outputFileType = AVFileTypeQuickTimeMovie

exporter?.shouldOptimizeForNetworkUse = true

exporter?.exportAsynchronously(completionHandler: {() -> Void in

DispatchQueue.main.async(execute: {() -> Void in

if exporter?.status == .completed {

let URL: URL? = exporter?.outputURL

let Avasset:AVAsset = AVAsset(url: URL!)

completion(Avasset)

}

else if exporter?.status == .failed{

print("Failed")

}

})

}) }

相关文章
|
JavaScript
Fastadmin列表的多图预览(一行代码)
Fastadmin列表的多图预览(一行代码)
384 0
|
7月前
小清新卡通人物404错误页面模板源码
小清新卡通人物404错误页面模板源码
47 5
小清新卡通人物404错误页面模板源码
|
前端开发 JavaScript
uniapp上传图片至服务器,获得在线图片链接预览(实战)
uniapp上传图片至服务器,获得在线图片链接预览(实战)
444 0
|
JavaScript 前端开发
前端js上传照片实现可预览功能
前端js上传照片实现可预览功能
68 0
|
小程序
微信小程序如何实现多图上传及预览删除
微信小程序如何实现多图上传及预览删除
124 0
|
小程序 前端开发
微信小程序_自定义markdown的图片点击放大处理
微信小程序_自定义markdown的图片点击放大处理
402 0
|
iOS开发 开发者 索引
批量上传iOS应用程序截图的实用技巧
提交iOS应用程序截图到iTunes Connect是一项非常繁琐的任务,因为你必须上传多达数十张屏幕截图,这是一个重复而枯燥的过程。但是,我们有一个好消息要告诉开发者们,现在有一个工具可以帮助你批量上传,方便快捷。
|
JavaScript 小程序 前端开发
微信小程序生成海报图片导出相册
小程序内通过静态模板和样式绘制 canvas ,导出图片,可用于生成分享图等场景
603 1
微信小程序生成海报图片导出相册
|
JavaScript 开发者
ElUpload不好用?一文教你实现一个简易图片上传预览组件
ElUpload不好用?一文教你实现一个简易图片上传预览组件
427 0