GPUImageUIElement叠加大小

时间:2016-02-23 09:48:47

标签: ios swift gpuimage uielement

我正在尝试使用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()

结果显示如下: Screenshot

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

谢谢!

1 个答案:

答案 0 :(得分:4)

Hello Andrey Gershengoren,

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

如果选择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()
    }


}

结果是打击:

enter image description here