GPUImageUIElement 叠加大小

GPUImageUIElement overlay size

我正在尝试使用 GPUImage 库录制带有一些叠加文本的视频。到目前为止,使用覆盖进行录制是可行的,但我无法使覆盖具有适当的大小。无论我通过初始化使用哪一帧,叠加视图始终占据预览层 (GPUImageView) 的整个大小。我尝试在情节提要和编程方式中添加叠加层。这是我的代码:

camera = GPUImageVideoCamera(sessionPreset: AVCaptureSessionPresetHigh, cameraPosition: AVCaptureDevicePosition.Back)
camera.outputImageOrientation = UIInterfaceOrientation.LandscapeRight
camera.horizontallyMirrorFrontFacingCamera = false
camera.horizontallyMirrorRearFacingCamera = false

filterView = self.view as! GPUImageView

filter = GPUImageBrightnessFilter()
blendFilter = GPUImageAlphaBlendFilter()
blendFilter.mix = 1.0

camera.addTarget(filter)

// here I try to add a label as UIElement
let label = UILabel(frame: CGRect(x: 10, y: 10, width: 100, height: 30))
label.text = "Demo text"
label.textColor = UIColor.redColor()
label.font = UIFont.systemFontOfSize(17.0)
label.backgroundColor = UIColor.clearColor()
view.addSubview(label) // for test purposes

uiElementInput = GPUImageUIElement(view: label)

filter.addTarget(blendFilter)
uiElementInput.addTarget(blendFilter)
blendFilter.addTarget(filterView)

filter.frameProcessingCompletionBlock = { filter, time in
  self.uiElementInput.update()
}

camera.startCameraCapture()

结果是这样的:

如何让 UIElement 像标签一样作为视图的一部分具有更小的尺寸?这个想法是添加一个预定义的覆盖视图(不仅仅是一个标签),但它也有错误的尺寸。

谢谢!

你好,安德烈·格申戈伦,

您应该将标签视图的大小设置为与相机捕获会话预设大小相同。

如果选择 AVCaptureSessionPreset640x480,则应将标签的大小设置为 (640, 480)。

我给你写了代码。

import UIKit
import GPUImage

class ViewController: UIViewController {

    @IBOutlet weak var filterView: GPUImageView!
    var videoCamera : GPUImageVideoCamera!
    var uiElement: GPUImageUIElement!
    var filter:GPUImageBrightnessFilter!
    var blendFilter: GPUImageAlphaBlendFilter!
    var uiElementInput: GPUImageUIElement!

    override func viewDidLoad() {
        super.viewDidLoad()
        videoCamera = GPUImageVideoCamera(
            sessionPreset: AVCaptureSessionPreset640x480,
            cameraPosition: AVCaptureDevicePosition.Back)
        videoCamera.outputImageOrientation = .Portrait
        videoCamera.horizontallyMirrorFrontFacingCamera = false
        videoCamera.horizontallyMirrorRearFacingCamera = false

        filter = GPUImageBrightnessFilter()
        blendFilter = GPUImageAlphaBlendFilter()
        blendFilter.mix = 1.0

        videoCamera.addTarget(filter)

        // here I try to add a label as UIElement
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: 640, height: 480))
        label.text = "Demo text"
        label.textColor = UIColor.redColor()
        label.font = UIFont.systemFontOfSize(17.0)
        label.backgroundColor = UIColor.clearColor()
        label.textAlignment = .Center

        uiElementInput = GPUImageUIElement(view: label)

        filter.addTarget(blendFilter)
        uiElementInput.addTarget(blendFilter)
        blendFilter.addTarget(filterView)

        filter.frameProcessingCompletionBlock = { filter, time in
            self.uiElementInput.update()
        }

        videoCamera.startCameraCapture()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


}

结果一击: