gpt4 book ai didi

ios - 更改 UIView 在 captureOutput 中的位置

转载 作者:行者123 更新时间:2023-11-28 08:18:07 24 4
gpt4 key购买 nike

目前,我有一个 AVCaptureSession 在我的 Swift 应用程序中运行,我有一个带有 CGRect 的 UIView,我想在相机源上四处移动。我一直在尝试通过更改 captureOutput 函数中的坐标来更新 UIView 框架的位置,但是当我更改它们时,它们不会更新。更改 UIView 的位置以使其即时更新的正确方法是什么?

这是进行捕获 session 的代码部分:

class CameraViewController: UIViewController, UIImagePickerControllerDelegate, AVCaptureVideoDataOutputSampleBufferDelegate

{

@IBOutlet weak var cameraView: UIImageView!
var rectangleView : UIView!
var captureSession : AVCaptureSession?
var previewLayer : AVCaptureVideoPreviewLayer?
var rect = CGRect(x: 150, y: 150, width: 50, height: 50)
var xCoord: Float = 0.0

override func viewDidLoad() {
super.viewDidLoad()

//---editing rectangle view---
rectangleView = UIView()
rectangleView.layer.borderColor = UIColor(hue: 0.2444, saturation: 1, brightness: 0.96, alpha: 1).cgColor
rectangleView.layer.borderWidth = 50;
rectangleView.tag = 1
self.view.addSubview(rectangleView)
//----------------------------


captureSession = AVCaptureSession()
captureSession?.sessionPreset = AVCaptureSessionPreset1920x1080

let backCamera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

var input = AVCaptureDeviceInput()
do{
input = try AVCaptureDeviceInput(device: backCamera)
}catch{
print("Error finding back Camera")
}
var error : NSError?

if (error == nil && (captureSession?.canAddInput(input))!)
{
captureSession?.addInput(input)

let dataOutput = AVCaptureVideoDataOutput()
dataOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "string buffer delegate"))

if (captureSession?.canAddOutput(dataOutput))!
{

captureSession?.addOutput(dataOutput)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
// cameraView.layer.addSublayer(previewLayer!)
self.view.layer.addSublayer(previewLayer!)
self.view.bringSubview(toFront: rectangleView)
previewLayer?.frame = self.view.bounds
rectangleView.frame = rect
captureSession?.startRunning()

}
}
}

这是 captureOutput 函数:setY 方法是 UIView 类的扩展,可更改框架的 y 坐标。

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {

let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer!)
let x = rectangleView.frame.origin.x
let y = rectangleView.frame.origin.y - 1
let height = rectangleView.frame.size.height
let width = rectangleView.frame.size.width

rectangleView.setY(y: y)

print(rectangleView.frame.origin.y)

//*** Making UIImage out of pixelBuffer ****
let ciImage = CIImage(cvPixelBuffer: pixelBuffer!)
let pixelBufferWidth = CGFloat(CVPixelBufferGetWidth(pixelBuffer!))
let pixelBufferHeight = CGFloat(CVPixelBufferGetHeight(pixelBuffer!))
let imageRect:CGRect = CGRect(x: 0,y: 0,width: pixelBufferWidth, height: pixelBufferHeight)
let ciContext = CIContext.init()
let cgimage = ciContext.createCGImage(ciImage, from: imageRect )
let image = UIImage(cgImage: cgimage!)
//******************************************



}

感谢您的帮助,

-nfarrell

最佳答案

您需要在主线程上调用 setY(y:) 方法,因为它正在操纵 UI:

DispatchQueue.main.async {
self.rectangleView.setY(y: y)
}

关于ios - 更改 UIView 在 captureOutput 中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42034562/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com