导出时,CALayer未正确设置为视频上的拖动位置

时间:2017-10-26 07:48:25

标签: ios swift avfoundation calayer

我正在尝试使用CALayer在视频上放置文字,我有一个可拖动的视图,所以我想根据视频分辨率导出带有精确位置和文字大小的文字视频,

enter image description here

现在,当我使用此位置导出视频时,结果视频看起来像这个

enter image description here

    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]

1 个答案:

答案 0 :(得分:0)

您应该将文本标签的坐标转换为视频帧坐标系。

例如,您的videoLayer帧是(0,0,200,200)

真实视频帧可以是(0,0,640,...)

在这种情况下,带有坐标(100,100)的文字标签将在视频中向上移动并保留。标签坐标应为(300,300)以保持与视图中相同的位置