gpt4 book ai didi

ios - 在应用程序(Swift 3)中的相机 View 上而不是在相机 View 下添加对象?

转载 作者:行者123 更新时间:2023-11-29 00:27:49 27 4
gpt4 key购买 nike

我在由红色圆圈组成的相机 View 上有一个叠加层,存储在 Assets.xcasset ImgOverlay 占位符中,相机 View (预览)出现在叠加层的后面或下面。没关系。应该如此。当我在 iPhone 上运行该应用程序时,叠加层会正常显示。参见图 1 但我也绘制了一个蓝色圆圈,它在相机 View 下方,只有在相机 View 丢失时才会出现。即,关闭或在模拟器上运行。请参见下面的 Img.2。

图 1. 相机 View 上的红色圆圈,在 iPhone 中

Image 1. red circles over camera view, in iPhone

图 2. 红色圆圈,包括在模拟器中绘制的蓝色圆圈 Image 2. red circles including drawn blue circle in Simulator
我到目前为止的代码在这里。我做错了什么,但看不到。我需要在 iPhone 中看到蓝色圆圈,而不仅仅是在模拟器中。我正在尝试绘制所有圆圈,因此我可以放弃 image.png 类型文件中的红色圆圈。我更喜欢在任何设备上绘制圆圈以获得更高的准确性,并显示它们而不是红色圆圈,然后保存圆圈和相机 View 的合成图像。我还没有设法在保存时组合图像,但第一步是在 iPhone 上看到蓝色圆圈......所以它几乎完全正常工作。我看不出我做错了什么?

ViewController.swift

import UIKit
import AVFoundation
import Foundation

class ViewController: UIViewController {

@IBOutlet weak var navigationBar: UINavigationBar!
@IBOutlet weak var imgOverlay: UIImageView!
@IBOutlet weak var btnCapture: UIButton!

@IBOutlet weak var shapeLayer: UIView!

let captureSession = AVCaptureSession()
let stillImageOutput = AVCaptureStillImageOutput()
var previewLayer : AVCaptureVideoPreviewLayer?

//var shapeLayer : CALayer?

// If we find a device we'll store it here for later use
var captureDevice : AVCaptureDevice?

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//=======================

let midX = self.view.bounds.midX
let midY = self.view.bounds.midY

let circlePath = UIBezierPath(arcCenter: CGPoint(x: midX,y: midY), radius: CGFloat(20), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)

let shapeLayer = CAShapeLayer()

shapeLayer.path = circlePath.cgPath
//change the fill color
shapeLayer.fillColor = UIColor.clear.cgColor
//you can change the stroke color
shapeLayer.strokeColor = UIColor.blue.cgColor
//you can change the line width
shapeLayer.lineWidth = 2.5

view.layer.addSublayer(shapeLayer)
print("Shape layer drawn")
//=====================
captureSession.sessionPreset = AVCaptureSessionPresetHigh

if let devices = AVCaptureDevice.devices() as? [AVCaptureDevice] {
// Loop through all the capture devices on this phone
for device in devices {
// Make sure this particular device supports video
if (device.hasMediaType(AVMediaTypeVideo)) {
// Finally check the position and confirm we've got the back camera
if(device.position == AVCaptureDevicePosition.back) {
captureDevice = device
if captureDevice != nil {
print("Capture device found")
beginSession()
}
}
}
}
}
}

@IBAction func actionCameraCapture(_ sender: AnyObject) {

print("Camera button pressed")
saveToCamera()
}

func beginSession() {

do {
try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice))
stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]

if captureSession.canAddOutput(stillImageOutput) {
captureSession.addOutput(stillImageOutput)
}

}
catch {
print("error: \(error.localizedDescription)")
}

guard let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) else {
print("no preview layer")
return
}
// this is what displays the camera view. But - it's on TOP of the drawn view, and under the overview. ??
self.view.layer.addSublayer(previewLayer)
previewLayer.frame = self.view.layer.frame



captureSession.startRunning()
print("Capture session running")


self.view.addSubview(navigationBar)
self.view.addSubview(imgOverlay)
self.view.addSubview(btnCapture)
}

func saveToCamera() {

if let videoConnection = stillImageOutput.connection(withMediaType: AVMediaTypeVideo) {
stillImageOutput.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (CMSampleBuffer, Error) in

if let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(CMSampleBuffer) {
if let cameraImage = UIImage(data: imageData) {


UIImageWriteToSavedPhotosAlbum(cameraImage, nil, nil, nil)

}
}
})
}
}



override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

最佳答案

你非常接近...

你有:

@IBOutlet weak var shapeLayer: UIView!

但随后您还创建了一个名为 shapeLayer 的 CAShapeLayer:

let shapeLayer = CAShapeLayer()

您将其添加为“主” View 的子层。然后,您将所有其他内容添加到您的主视图之上,覆盖 shapeLayer。

在 viewDidLoad() 中,将您的蓝色圆圈绘图部分更改为:

    let midX = self.view.bounds.midX
let midY = self.view.bounds.midY

let circlePath = UIBezierPath(arcCenter: CGPoint(x: midX,y: midY), radius: CGFloat(20), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)

let shapeLayerPath = CAShapeLayer()

shapeLayerPath.path = circlePath.cgPath
//change the fill color
shapeLayerPath.fillColor = UIColor.clear.cgColor
//you can change the stroke color
shapeLayerPath.strokeColor = UIColor.blue.cgColor
//you can change the line width
shapeLayerPath.lineWidth = 2.5

// add the blue-circle layer to the shapeLayer ImageView
shapeLayer.layer.addSublayer(shapeLayerPath)

print("Shape layer drawn")
//=====================

然后,在 beginSession() 结束时...

    self.view.addSubview(navigationBar)
self.view.addSubview(imgOverlay)
self.view.addSubview(btnCapture)

// shapeLayer ImageView is already a subview created in IB
// but this will bring it to the front
self.view.addSubview(shapeLayer)

// note: since these elements are added as @IBOutlet in IB,
// these could be:

// self.view.bringSubview(toFront: navigationBar)
// self.view.bringSubview(toFront: imgOverlay)
// self.view.bringSubview(toFront: btnCapture)
// self.view.bringSubview(toFront: shapeLayer)

看看你得到了什么:)

编辑:后续问题移至 Combining Images in CameraView with Overlay. (Swift 3)?

关于ios - 在应用程序(Swift 3)中的相机 View 上而不是在相机 View 下添加对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42579545/

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