>通过AVAssetTrack设置自然大小.
>在AVMutableVIDeoComposition renderFrame中将视频约束为纵向大小.
>将新视频边界锁定到录制的视频宽度和高度.
下面的代码与我需要帮助的问题不同.我正在尝试添加的图像覆盖了整个纵向视图,并且边缘周围都有边框.该应用程序也只允许肖像.
func processVIDeoWithWatermark(vIDeo: AVURLAsset,watermark: UIImage,completion: @escaPing (Bool) -> VoID) { let composition = AVMutableComposition() let asset = AVURLAsset(url: vIDeo.url,options: nil) let track = asset.tracks(withMediaType: AVMediaTypeVIDeo) let vIDeoTrack:AVAssetTrack = track[0] as AVAssetTrack let timerange = CMTimeRangeMake(kCMTimeZero,asset.duration) let compositionVIDeoTrack:AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaTypeVIDeo,preferredTrackID: CMPersistentTrackID()) do { try compositionVIDeoTrack.insertTimeRange(timerange,of: vIDeoTrack,at: kCMTimeZero) compositionVIDeoTrack.preferredtransform = vIDeoTrack.preferredtransform } catch { print(error) }// let compositionAudioTrack:AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaTypeAudio,preferredTrackID: CMPersistentTrackID())// // for audioTrack in asset.tracks(withMediaType: AVMediaTypeAudio) {// do {// try compositionAudioTrack.insertTimeRange(audioTrack.timeRange,of: audioTrack,at: kCMTimeZero)// } catch {// print(error)// }// // }// let size = vIDeoTrack.naturalSize let watermark = watermark.cgImage let watermarklayer = CALayer() watermarklayer.contents = watermark watermarklayer.frame = CGRect(x: 0,y: 0,wIDth: screenWIDth,height: screenHeight) watermarklayer.opacity = 1 let vIDeolayer = CALayer() vIDeolayer.frame = CGRect(x: 0,height: screenHeight) let parentlayer = CALayer() parentlayer.frame = CGRect(x: 0,wIDth: size.wIDth,height: size.height) parentlayer.addSublayer(vIDeolayer) parentlayer.addSublayer(watermarklayer) let layercomposition = AVMutableVIDeoComposition() layercomposition.frameDuration = CMTimeMake(1,30) layercomposition.renderSize = CGSize(wIDth: screenWIDth,height: screenHeight) layercomposition.renderScale = 1.0 layercomposition.animationTool = AVVIDeoCompositionCoreAnimationTool(postProcessingAsVIDeolayer: vIDeolayer,in: parentlayer) let instruction = AVMutableVIDeoCompositionInstruction() instruction.timeRange = CMTimeRangeMake(kCMTimeZero,composition.duration) let vIDeotrack = composition.tracks(withMediaType: AVMediaTypeVIDeo)[0] as AVAssetTrack let layerinstruction = AVMutableVIDeoCompositionLayerInstruction(assetTrack: vIDeotrack) layerinstruction.settransform(vIDeoTrack.preferredtransform,at: kCMTimeZero) instruction.layerInstructions = [layerinstruction] layercomposition.instructions = [instruction] let filePath = NstemporaryDirectory() + self.filename() let movIEUrl = URL(fileURLWithPath: filePath) guard let assetExport = AVAssetExportSession(asset: composition,presetname:AVAssetExportPresetHighestQuality) else {return} assetExport.vIDeoComposition = layercomposition assetExport.outputfileType = AVfileTypeMPEG4 assetExport.outputURL = movIEUrl assetExport.exportAsynchronously(completionHandler: { switch assetExport.status { case .completed: print("success") print(vIDeo.url) self.saveVIDeoToUserlibrary(fileURL: movIEUrl,completion: { (success,error) in if success { completion(true) } else { completion(false) } }) break case .cancelled: print("cancelled") break case .exporting: print("exporting") break case .Failed: print(vIDeo.url) print("Failed: \(assetExport.error!)") break case .unkNown: print("unkNown") break case .waiting: print("waiting") break } })}解决方法 如果视频图层应填充父图层,则您的vIDeolayer的帧不正确.您需要将大小设置为size而不是screenSize. 总结
以上是内存溢出为你收集整理的ios – 在Swift 3中为视频添加叠加层全部内容,希望文章能够帮你解决ios – 在Swift 3中为视频添加叠加层所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)