从AVAssetExportSession导出视频时,CATextlayer字体大小不会更改

时间:2017-06-19 12:59:29

标签: ios iphone swift3 calayer

我正在使用CATextLayer在视频上添加文字。但是当导出过程完成文本大小没有按帧位置改变时。字体大小打印在日志中但不会在CATextLayer fontsize中更改。这是我所做的代码。

let mainCompositionInst = AVMutableVideoComposition()
    var naturalSize = CGSize()
    naturalSize = CGSize(width: (videoAssetTrack?.naturalSize.width)!, height:(videoAssetTrack?.naturalSize.height)!)
    var renderWidth = CGFloat()
    var renderHeight = CGFloat()

    renderWidth = naturalSize.width
    renderHeight = naturalSize.height

    mainCompositionInst.renderSize = CGSize(width: renderWidth, height: renderHeight)
    mainCompositionInst.instructions = [mainInstruction]
    mainCompositionInst.frameDuration = CMTimeMake(1, 30)

let myAttributes = [
        NSFontAttributeName: UIFont(name: "HelveticaNeue-Thin", size: 30)!, // font
        NSForegroundColorAttributeName: UIColor.white                   // text color
    ]
    let myAttributedString = NSAttributedString(string: "Title", attributes: myAttributes )

    let titleLayer = CATextLayer()
    titleLayer.string = myAttributedString

    titleLayer.frame = CGRect(x: (dragView?.frame.origin.x)!, y: (dragView?.frame.origin.y)!, width: (dragView?.frame.size.width)!, height: (dragView?.frame.height)!)
    titleLayer.shadowOpacity = 0.5
    titleLayer.isWrapped = true


    print("Font Size \(String(describing: titleLayer.fontSize))")
    let overlayLayer = CALayer()
    overlayLayer.addSublayer(titleLayer)
    overlayLayer.frame = CGRect(x: 0, y: 0, width: naturalSize.width, height: naturalSize.height)
    overlayLayer.masksToBounds = true

    let parentLayer = CALayer()
    let videoLayer = CALayer()
    parentLayer.frame = CGRect(x: 0, y: 0, width: naturalSize.width, height: naturalSize.height)
    videoLayer.frame = CGRect(x: 0, y: 0, width: naturalSize.width, height: naturalSize.height)


    parentLayer.addSublayer(videoLayer)
    parentLayer.addSublayer(overlayLayer)

    mainCompositionInst.animationTool = AVVideoCompositionCoreAnimationTool.init(postProcessingAsVideoLayer: videoLayer, in: parentLayer)

1 个答案:

答案 0 :(得分:0)

只添加了一行,代码工作正常titleLayer.shouldRasterize = true