LoginSignup
27
27

More than 5 years have passed since last update.

[Swift]AVFoundationでカメラを使う

Last updated at Posted at 2015-11-22

ソース元

https://akira-watson.com/iphone/camera.html
https://sites.google.com/a/gclue.jp/swift-docs/ni-yinki100-ios/3-avfoundation/002-kamerano-qi-dongto-hua-xiangno-bao-cun

この2つのソースコードを使いました。

コード

ViewController

import UIKit
import AVFoundation

class ViewController: UIViewController {

    //カメラセッション
    var captureSession: AVCaptureSession!
    //デバイス
    var cameraDevices: AVCaptureDevice!
    //画像のアウトプット
    var imageOutput: AVCaptureStillImageOutput!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        //セッションの作成
        captureSession = AVCaptureSession()

        //デバイス一覧の取得
        let devices = AVCaptureDevice.devices()

        //バックカメラをcameraDevicesに格納
        for device in devices {
            if device.position == AVCaptureDevicePosition.Back {
                cameraDevices = device as! AVCaptureDevice
            }
        }

        //バックカメラからVideoInputを取得
        let videoInput: AVCaptureInput!
        do {
            videoInput = try AVCaptureDeviceInput.init(device: cameraDevices)
        } catch {
            videoInput = nil
        }

        //セッションに追加
        captureSession.addInput(videoInput)

        //出力先を生成
        imageOutput = AVCaptureStillImageOutput()

        //セッションに追加
        captureSession.addOutput(imageOutput)

        //画像を表示するレイヤーを生成
        let captureVideoLayer: AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer.init(session: captureSession)
        captureVideoLayer.frame = self.view.bounds
        captureVideoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill

        //Viewに追加
        self.view.layer.addSublayer(captureVideoLayer)

        //セッション開始
        captureSession.startRunning()

    }

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

    @IBAction func cameraStart(sender: AnyObject) {
        //ビデオ出力に接続
        let captureVideoConnection = imageOutput.connectionWithMediaType(AVMediaTypeVideo)

        //接続から画像を取得
        self.imageOutput.captureStillImageAsynchronouslyFromConnection(captureVideoConnection) { (imageDataBuffer, error) -> Void in
            //取得したImageのDataBufferをJPEGを変換
            let capturedImageData: NSData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataBuffer)
            //JPEGからUIImageを作成
            let Image: UIImage = UIImage(data: capturedImageData)!
            //アルバムに追加
            UIImageWriteToSavedPhotosAlbum(Image, self, nil, nil)
        }
    }


}

GitHub

27
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
27