在iOS中播放视频到广场[Swift 3]

在iOS中播放视频到广场[Swift 3],第1张

概述我无法使用 Swift 3和iOS 10.x裁剪视频.在我执行裁剪程序后,我将视频保存到我的照片库中,它看起来与原始照片相同. 我使用以下帖子作为参考:Cropping AVAsset video with AVFoundation not working iOS 8 func suqareCropVideo(inputURL: NSURL, completion: @escaping (_ ou 我无法使用 Swift 3和iOS 10.x裁剪视频.在我执行裁剪程序后,我将视频保存到我的照片库中,它看起来与原始照片相同.

我使用以下帖子作为参考:Cropping AVAsset video with AVFoundation not working iOS 8

func suqareCropVIDeo(inputURL: NSURL,completion: @escaPing (_ outputURL : NSURL?) -> ()){    let vIDeoAsset: AVAsset = AVAsset( url: inputURL as URL )    let clipVIDeoTrack = vIDeoAsset.tracks( withMediaType: AVMediaTypeVIDeo ).first! as AVAssetTrack    let composition = AVMutableComposition()    composition.addMutableTrack(withMediaType: AVMediaTypeVIDeo,preferredTrackID: CMPersistentTrackID())    let vIDeoComposition = AVMutableVIDeoComposition()    vIDeoComposition.renderSize = CGSize( wIDth: clipVIDeoTrack.naturalSize.height,height: clipVIDeoTrack.naturalSize.height )    vIDeoComposition.frameDuration = CMTimeMake(1,30)    let transformer = AVMutableVIDeoCompositionLayerInstruction(assetTrack: clipVIDeoTrack)    let instruction = AVMutableVIDeoCompositionInstruction()    instruction.timeRange = CMTimeRangeMake(kCMTimeZero,CMTimeMakeWithSeconds(60,30))    let transform1: CGAffinetransform = CGAffinetransform(translationX: clipVIDeoTrack.naturalSize.height,y: (clipVIDeoTrack.naturalSize.wIDth - clipVIDeoTrack.naturalSize.height) / 2)    let transform2 = transform1.rotated(by: .pi/2)    let finaltransform = transform2    transformer.settransform(finaltransform,at: kCMTimeZero)    instruction.layerInstructions = [transformer]    vIDeoComposition.instructions = [instruction]    // Export    let exportSession = AVAssetExportSession(asset: vIDeoAsset,presetname: AVAssetExportPresetHighestQuality)!    print ("random ID = \(NSUUID().uuIDString)")    let croppedOutputfileUrl = URL( fileURLWithPath: getoutputPath( NSUUID().uuIDString) ) // CREATE RANDOM file name HERE    exportSession.outputURL = croppedOutputfileUrl    exportSession.outputfileType = AVfileTypeQuickTimeMovIE    exportSession.exportAsynchronously() { handler -> VoID in        if exportSession.status == .completed {            print("Export complete")            dispatchQueue.main.async(execute: {                completion(croppedOutputfileUrl as NSURL)            })            return        } else if exportSession.status == .Failed {            print("Export Failed - \(String(describing: exportSession.error))")        }        completion(nil)        return    }}

用法如下:

if vIDeoURL != nil {        print ("crop vIDeo")        suqareCropVIDeo(inputURL: self.vIDeoURL,completion: { (outputURL) -> () in            print ("compressed url = \(String(describing: outputURL))")            // Save vIDeo to photo library            PHPhotolibrary.shared().performChanges({                PHAssetChangeRequest.creationRequestForAssetFromVIDeo(atfileURL:outputURL! as URL)            }) { saved,error in                if saved {                    print ("save successful")                }                else {                    print ("save Failed")                }            }        })    }
解决方法 您实际上并未在导出器上设置视频合成

所以试试吧

exportSession.vIDeoComposition = vIDeoComposition

在开始出口之前.

总结

以上是内存溢出为你收集整理的在iOS中播放视频广场[Swift 3]全部内容,希望文章能够帮你解决在iOS中播放视频到广场[Swift 3]所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1062994.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存