1

I try create a camera like Snapchat with swift 3 in ios 10.2 but the problem is when I run the app in my iPhone. The UIView appear empty. This is my code.

import UIKit
import AVFoundation

class TakeSelfieViewController: UIViewController {

@IBOutlet var cameraView: UIView!

var captureSession : AVCaptureSession?
var stillImageOutput: AVCaptureStillImageOutput?
var previewLayer : AVCaptureVideoPreviewLayer?

override func viewDidLoad() {
    super.viewDidLoad()

    captureSession = AVCaptureSession()
    captureSession?.sessionPreset = AVCaptureSessionPreset1920x1080
    let deviceDiscoverySession = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDuoCamera, AVCaptureDeviceType.builtInTelephotoCamera,AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.front)
    for device in (deviceDiscoverySession?.devices)! {
        if device.position == AVCaptureDevicePosition.front{
            do {
                let input = try AVCaptureDeviceInput(device: device)
                if (captureSession?.canAddInput(input))!{
                    captureSession?.addInput(input)
                    stillImageOutput = AVCaptureStillImageOutput()
                    stillImageOutput?.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
                    if (captureSession?.canAddOutput(stillImageOutput))! {
                        captureSession?.addOutput(stillImageOutput)
                        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                        previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
                        previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
                        cameraView.layer.addSublayer(previewLayer!)
                        captureSession?.startRunning()
                    }
                }
            } catch{
                print("Error Occured when trying get camera")
            }
        }
    }

}

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

override var prefersStatusBarHidden: Bool {

    return true

}

In my Info.plist I have added the Privacy - Camera Usage Description

What's happening?

Bluewings
  • 3,438
  • 3
  • 18
  • 31
sebas.varela
  • 1,535
  • 2
  • 10
  • 12
  • Do add the description correctly ? – BilalReffas Jan 19 '17 at 20:11
  • Look at this http://stackoverflow.com/questions/37869963/how-to-use-avcapturephotooutput/38171414#38171414 – BilalReffas Jan 19 '17 at 20:11
  • there is a simple class which is a camera manager and can handle your camera session , you have to set the view you want to appear the camera previewlayer in it : https://github.com/maryamfekri/MFCameraManager – Maryam Fekri Feb 07 '17 at 11:05

2 Answers2

0

Taking photo is very easy using swift3 code in ios 10 and later vesions Just look here First you need to present the image picker.In ios 10 onwards you should get permission to access the camera and photos from library .

you can added these delgates to your class

            import UIKit

         class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

Info.plist file you can get accesses these things 1.Privacy-photo library usage descriptin 2.Privacy-Camera usage access

        func cameraAction(_ sender: AnyObject) {
              if UIImagePickerController 
        .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
        let imagePicker = UIImagePickerController()
        imagePicker.delegate=self
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera
        imagePicker.allowsEditing = true
        self .present(imagePicker, animated: true, completion: nil)
    }

  }

Here yoou can take UIImageView in bellow code imageView it is used to show the image what your selected image from gallery or from camera

  func imagePickerController(_ picker: UIImagePickerController,    didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage{
        imageView.image = image
        imageView.contentMode = .scaleAspectFit
        self.dismiss(animated: true, completion: nil)
    }

}

you can get photos from librarys in your mobile gallery

       func photosFromLibrary(_ sender: AnyObject) {
    if UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
        let imagePicker = UIImagePickerController()
        imagePicker.delegate=self
        imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
        imagePicker.allowsEditing = true
        self .present(imagePicker, animated: true, completion: nil)
    }

}
0

You need to set the frame of the previewLayer.

previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer?.frame = cameraView.frame // this line is new
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect