Ios 无法获取相机图像
我创建了一个简单的应用程序,该应用程序可以从iPhone摄像头向视图显示实时图像,修改了本教程,该教程是用Swift2编写成Swift3的 我使用Xcode 8.1,在iOS版本为9.3.4的iPhone 6上运行该应用程序 我的Xcode部署目标设置是9.1 当我运行应用程序时,代码完成,没有错误,但相机图像没有显示。该应用程序甚至没有显示使用摄像头的权限警报 我做错了什么 以下是我的完整代码Ios 无法获取相机图像,ios,iphone,swift,camera,Ios,Iphone,Swift,Camera,我创建了一个简单的应用程序,该应用程序可以从iPhone摄像头向视图显示实时图像,修改了本教程,该教程是用Swift2编写成Swift3的 我使用Xcode 8.1,在iOS版本为9.3.4的iPhone 6上运行该应用程序 我的Xcode部署目标设置是9.1 当我运行应用程序时,代码完成,没有错误,但相机图像没有显示。该应用程序甚至没有显示使用摄像头的权限警报 我做错了什么 以下是我的完整代码 import UIKit import AVFoundation class ViewContro
import UIKit
import AVFoundation
class ViewController: UIViewController {
// MARK: UI
let cameraView: UIView = {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
// MARK: System
var captureSession = AVCaptureSession()
var sessionOutput = AVCaptureStillImageOutput()
var previewLayer = AVCaptureVideoPreviewLayer()
override func viewDidLoad() {
super.viewDidLoad()
setupSubviews()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
cameraSetup()
}
func setupSubviews() {
view.addSubview( cameraView )
cameraView.backgroundColor = UIColor.orange
cameraView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
cameraView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
cameraView.widthAnchor.constraint(equalToConstant: 200).isActive = true
cameraView.heightAnchor.constraint(equalToConstant: 200).isActive = true
}
func cameraSetup() {
print("**** Start Camera Setup ****")
let devices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo)! as! [AVCaptureDevice]
for device in devices {
if device.position == .front {
print("Front")
do {
let input = try AVCaptureDeviceInput(device: device)
if captureSession.canAddInput(input) {
captureSession.addInput(input)
sessionOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
print("Input added")
if captureSession.canAddOutput(sessionOutput) {
captureSession.addOutput(sessionOutput)
captureSession.startRunning()
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.portrait
cameraView.layer.addSublayer(previewLayer)
previewLayer.position = CGPoint(x: self.cameraView.frame.width / 2, y: self.cameraView.frame.height / 2)
previewLayer.bounds = cameraView.frame
print("Output added")
}
}
} catch {
print("Error")
}
}
}
print("**** Finish Camera Setup ****")
}
}
从您的设备中删除应用程序并在设备中重新安装,如果您添加了摄像头权限,那么它肯定会要求。记住模拟器没有摄像头,所以请使用iPhone或ipad检查代码。您是否在plist中添加了
隐私-摄像头使用说明
?