gpt4 book ai didi

ios - 当viewWillAppear(再次)时重新初始化条形码阅读器

转载 作者:行者123 更新时间:2023-11-29 02:07:50 25 4
gpt4 key购买 nike

我有一个条形码阅读器,它在第一次加载 View 时正确初始化。但是,当我返回 View 时,视频源停止工作。

下面的代码显示了如何在 viewDidLoad 方法中初始化。关于如何修改它的任何建议,以便我可以在 viewWillAppear(再次)时调用它的一部分?

代码:

override func viewDidLoad() {
super.viewDidLoad()

// Get an instance of the AVCaptureDevice class to initialize a device object and provide the video
// as the media type parameter.
let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

// Get an instance of the AVCaptureDeviceInput class using the previous device object.
var error:NSError?
let input: AnyObject! = AVCaptureDeviceInput.deviceInputWithDevice(captureDevice, error: &error)

if (error != nil) {
// If any error occurs, simply log the description of it and don't continue any more.
println("\(error?.localizedDescription)")
return
}

// Initialize the captureSession object.
captureSession = AVCaptureSession()
// Set the input device on the capture session.
captureSession?.addInput(input as AVCaptureInput)

// Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
let captureMetadataOutput = AVCaptureMetadataOutput()
captureSession?.addOutput(captureMetadataOutput)

// Set delegate and use the default dispatch queue to execute the call back
captureMetadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
captureMetadataOutput.metadataObjectTypes = supportedBarCodes

// Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
//videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill

var tmpbounds=view.layer.bounds;

videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill;
videoPreviewLayer?.bounds = tmpbounds
videoPreviewLayer?.position = CGPointMake(CGRectGetMidX(tmpbounds), CGRectGetMidY(tmpbounds))

// videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspect

videoPreviewLayer?.frame = view.layer.bounds
view.layer.addSublayer(videoPreviewLayer)

// Start video capture.
captureSession?.startRunning()

// Move the message label to the top view
view.bringSubviewToFront(messageLabel)

// Initialize QR Code Frame to highlight the QR code
qrCodeFrameView = UIView()
qrCodeFrameView?.layer.borderColor = UIColor.greenColor().CGColor
qrCodeFrameView?.layer.borderWidth = 2
view.addSubview(qrCodeFrameView!)
view.bringSubviewToFront(qrCodeFrameView!)
}

最佳答案

尝试在viewDidAppear上执行

captureSession?.startRunning()

在 View 中会消失

captureSession?.stopRunning()

同时注册通知 AVCaptureSessionRuntimeErrorNotification 并记录所有错误。我认为这会帮助您了解问题出在哪里。

关于ios - 当viewWillAppear(再次)时重新初始化条形码阅读器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29604784/

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