customcamera's People
Forkers
carabina nmcdonaldd ahijaouy larryb58 galkeidar10 lloyddavid prudhvi111 mspvirajpatel rodrigocarrapeiro ibrdrahim shunchobi lunafang1016customcamera's Issues
Swift 4 has runtime errors with code
This is a great tutorial! I'm stuck on swift 4 syntax changes and I'm hoping you can help.
This line of code in ViewController prepareCamera function
if let availableDevices = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back).devices
Has a Buildtime error:
ViewController.swift:34:12: Initializer for conditional binding must have Optional type, not '[AVCaptureDevice]'
I've tried several things and this converted it from an Error to a Warning:
if let availableDevices = try?
AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back).devices
ViewController.swift:53:13: No calls to throwing functions occur within 'try' expression
This also happens with if let previewLayer
in the beginSession function
Preview not matching superview frame?
Hey Brian! Curious as to why setting the layers frame doesn't make it match the full screen parent view bounds? Trying to get a full screen camera preview. Any help would be great!!
Fix the issue that "viewWillAppear" not called when PhotoVC dissmissed.
Ref : https://stackoverflow.com/a/60295024/2151275
Need to set the modalPresentationStyle
of PhotoVC
to full screen.
Full code of ViewController.swift
import UIKit
import AVFoundation
class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {
let captureSession = AVCaptureSession()
var previewLayer:CALayer!
var captrueDevice:AVCaptureDevice!
var takePhoto = false
override func viewDidLoad() {
super.viewDidLoad()
prepareCamera()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.captureSession.startRunning()
}
func prepareCamera() {
captureSession.sessionPreset = AVCaptureSession.Preset.photo
let availableDeviceSession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: .video, position: .back)
captrueDevice = availableDeviceSession.devices.first
beginSession()
}
func beginSession() {
do {
let captureDeviceInput = try AVCaptureDeviceInput(device: captrueDevice)
captureSession.addInput(captureDeviceInput)
} catch {
print(error.localizedDescription)
}
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
self.previewLayer = previewLayer
self.view.layer.addSublayer(self.previewLayer)
self.previewLayer.frame = self.view.layer.frame
captureSession.startRunning()
let dataOutput = AVCaptureVideoDataOutput()
dataOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as String): NSNumber(value: kCVPixelFormatType_32BGRA)]
captureSession.addOutput(dataOutput)
captureSession.commitConfiguration()
let queue = DispatchQueue(label: "com.czk.captureQueue")
dataOutput.setSampleBufferDelegate(self, queue: queue)
}
@IBAction func takePhoto(_ sender: Any) {
takePhoto = true
}
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
if takePhoto {
takePhoto = false
if let image = getImageFromSampleBuffer(buffer: sampleBuffer) {
DispatchQueue.main.async {
let photoVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "PhotoVC") as! PhotoViewController
photoVC.takenPhoto = image
photoVC.modalPresentationStyle = .fullScreen
self.present(photoVC, animated: true, completion: {
self.captureSession.stopRunning()
})
}
}
}
}
func getImageFromSampleBuffer(buffer:CMSampleBuffer) -> UIImage? {
if let pixelBuffer = CMSampleBufferGetImageBuffer(buffer) {
let ciImage = CIImage(cvPixelBuffer: pixelBuffer)
let context = CIContext()
let imageRect = CGRect(x: 0, y: 0, width: CVPixelBufferGetWidth(pixelBuffer), height: CVPixelBufferGetHeight(pixelBuffer))
if let image = context.createCGImage(ciImage, from: imageRect) {
return UIImage(cgImage: image, scale: UIScreen.main.scale, orientation: .right)
}
}
return nil
}
}
Problem with photoVC
in the line
photoVC.takenPhoto = image
the error of "Value of type 'PhotoViewController' has no member 'takenPhoto'" shows up as photoVC is initialized as a UIStoryBoard. I'm not sure if this error only occurs in newer versions of swift or not but I can't seem to get past it
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.