【发布时间】:2018-04-07 11:57:48
【问题描述】:
我正在尝试使用 CALayer 将文本放在视频上,我有一个可拖动的视图,所以我想根据视频分辨率导出带有精确位置和文本大小的文本的视频,
现在,当我用这个位置导出视频时,结果视频看起来像这样
let parentLayer = CALayer()
let videoLayer = CALayer()
parentLayer.frame = CGRect(x:0, y:0, width:clipVideoTrack.naturalSize.width,height: clipVideoTrack.naturalSize.height)
videoLayer.frame = CGRect(x:0, y:0, width:clipVideoTrack.naturalSize.width, height:clipVideoTrack.naturalSize.height)
let backgroundLayer = CALayer()
backgroundLayer.bounds = CGRect(x: 0, y: 0, width: clipVideoTrack.naturalSize.width, height: clipVideoTrack.naturalSize.height)
backgroundLayer.position = CGPointMake(clipVideoTrack.naturalSize.width / 2.0, clipVideoTrack.naturalSize.height / 2.0)
backgroundLayer.addSublayer(supplyView.layer)
backgroundLayer.masksToBounds = true
for (index, view) in views.enumerate(){
var layer = CALayer()
layer = view.layer
layer.frame = CGRect(x: supplyView.frame.origin.x, y: supplyView.frame.origin.y, width: supplyView.frame.size.width, height: supplyView.frame.size.height)
layer.beginTime = CFTimeInterval(Float(startingDurations[index]))
layer.duration = CFTimeInterval(Float(endingDurations[index]))
layer.setAffineTransform(view.transform)
layer.addAnimation(fadeAnimation, forKey: "scale")
backgroundLayer.addSublayer(layer)
print(index)
}
backgroundLayer.geometryFlipped = true
parentLayer.addSublayer(backgroundLayer)
let mainInstruction = AVMutableVideoCompositionInstruction()
mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, mixComposition.duration)
let videoLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)
let videoAssetTrack = mixComposition.tracksWithMediaType(AVMediaTypeVideo).first
videoLayerInstruction.setTransform((videoAssetTrack?.preferredTransform)!, atTime: kCMTimeZero)
videoLayerInstruction.setOpacity(0.0, atTime: videoAsset.duration)
mainInstruction.layerInstructions = [videoLayerInstruction]
【问题讨论】:
标签: ios swift avfoundation calayer