ecptavares
(Eduardo Tavares)
1
Hi!
I am building a custom camera in code but if I try my app on a device I don't see the shooting button (myButton) if I try it on a simulator I do see If I remove the code to setup the camera.
Although I don't see the button if I click on the spot the button was supposed to be I can take the picture.
Here is the code I am using ;
//
// ViewController.swift
// cam
//
// Created by Eduardo Tavares on 3/10/18.
// Copyright © 2018 Eduardo Tavares. All rights reserved.
//
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
var captureSession : AVCaptureSession?
var videoPreviewLayer : AVCaptureVideoPreviewLayer?
var capturePhotoOutput : AVCapturePhotoOutput?
let myView : UIView = {
let view = UIView()
view.backgroundColor = .lightGray
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let myButton : UIButton = {
let bt = UIButton()
bt.layer.cornerRadius = 35
bt.layer.masksToBounds = true
bt.backgroundColor = .white
bt.alpha = 0.5
bt.setTitle("TAKE", for: .normal)
bt.translatesAutoresizingMaskIntoConstraints = false
bt.addTarget(self, action: #selector(handleClick), for: .touchUpInside)
return bt
}()
override func viewDidLoad() {
super.viewDidLoad()
setupView()
setupCamera()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
@objc func handleClick() {
guard let capturePhotoOutput = self.capturePhotoOutput else { return }
let photoSettings = AVCapturePhotoSettings()
photoSettings.isAutoStillImageStabilizationEnabled = true
photoSettings.isHighResolutionPhotoEnabled = true
photoSettings.flashMode = .auto
capturePhotoOutput.capturePhoto(with: photoSettings, delegate: self)
}
func setupView() {
view.addSubview(myView)
myView.addSubview(myButton)
myView.bringSubview(toFront: myButton)
myView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
myView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 8).isActive = true
myView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 8).isActive = true
myView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
myButton.centerXAnchor.constraint(equalTo:myView.centerXAnchor).isActive = true
myButton.bottomAnchor.constraint(equalTo: myView.bottomAnchor,constant: -15).isActive = true
myButton.widthAnchor.constraint(equalToConstant: 70).isActive = true
myButton.heightAnchor.constraint(equalToConstant: 70).isActive = true
}
func setupCamera() {
let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)
do {
let input = try AVCaptureDeviceInput(device: captureDevice!)
captureSession = AVCaptureSession()
captureSession?.addInput(input)
} catch {
print(error)
}
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
videoPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
videoPreviewLayer?.frame = view.layer.bounds
view.layer.addSublayer(videoPreviewLayer!)
captureSession?.startRunning()
capturePhotoOutput = AVCapturePhotoOutput()
capturePhotoOutput?.isHighResolutionCaptureEnabled = true
captureSession?.addOutput(capturePhotoOutput!)
}
}
extension ViewController : AVCapturePhotoCaptureDelegate {
func photoOutput(_ captureOutput: AVCapturePhotoOutput,didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?,previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings,bracketSettings: AVCaptureBracketedStillImageSettings?,error: Error?) {
guard error == nil,let photoSampleBuffer = photoSampleBuffer else { return }
guard let imageData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else { return }
let captureImage = UIImage.init(data: imageData,scale: 1.0)
if let image = captureImage {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
}
}
What am I doing wrong?
thank you
saagarjha
(Saagar Jha)
2
Hello @ecptavares, welcome to the Swift Forums! While your question is an interesting one, unfortunately it isn't a great fit for this site, which is more aimed towards discussion of the Swift language itself rather than Apple's frameworks. I'd suggest reposting this question on StackOverflow or Apple's Developer Forums.