自定义相机视图Swift iOS 8 iPhone xCode 6.1

Ada*_*aka 7 iphone xcode camera ios swift

我想在我的iPhone里面使用相机.我不想使用典型的全屏摄像头视图,但我自己.

例如,我想在屏幕中间有一个200x200的正方形,并有一个摄像头预览.在这个广场下面,我想要一个按钮来拍照.怎么做?我很快开始了.

小智 10

您将希望使用AVFoundation框架允许您AVCaptureSession在故事板中创建自己的视图.这是一个很好的教程,向您展示如何找到相机并创建捕获会话:

http://jamesonquave.com/blog/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-1/

本教程使用整个视图作为捕获视图,因此如果您在代码之后对其进行建模,那么相机的大小.要在屏幕中间制作一个200x200的正方形,您必须在故事板中的视图控制器上绘制一个,将其链接到swift文件中所有代码所在的变量,然后更改底部的部分说的是,

previewLayer?.frame = self.view.layer.frame
Run Code Online (Sandbox Code Playgroud)

your200by200View.layer.frame

希望这可以提供帮助.如果没有,我可以尝试帮助更多,或者有人可以纠正我.

祝好运!


Dur*_*nat 5

另一个代码块.如何使用iPhone进行手动对焦.

import UIKit

class ViewController: UIViewController {

    @IBOutlet var cameraView: CameraView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    @IBAction func sliderChanged(sender: UISlider) {
        cameraView.setFocusWithLensPosition(sender.value)
    }
}


import UIKit
import AVFoundation

class CameraView: UIView {

    // AVFoundation properties
    let captureSession = AVCaptureSession()
    var captureDevice: AVCaptureDevice!
    var captureDeviceFormat: AVCaptureDeviceFormat?
    let stillImageOutput = AVCaptureStillImageOutput()
    var cameraLayer: AVCaptureVideoPreviewLayer?

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initCamera()
    }

    func initCamera() {
        captureSession.beginConfiguration()


        stillImageOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]

        // get the back camera
        if let device = cameraDeviceForPosition(AVCaptureDevicePosition.Back) {

            captureDevice = device
            captureDeviceFormat = device.activeFormat

            let error: NSErrorPointer = nil

            do {
                try captureDevice!.lockForConfiguration()
            } catch let error1 as NSError {
                error.memory = error1
            }
            captureDevice!.focusMode = AVCaptureFocusMode.Locked
            captureDevice!.unlockForConfiguration()

            var deviceInput: AVCaptureDeviceInput!
            do {
                deviceInput = try AVCaptureDeviceInput(device: captureDevice)
            } catch let error1 as NSError {
                error.memory = error1
                deviceInput = nil
            }
            if(error == nil) {
                captureSession.addInput(deviceInput)
            }

            captureSession.addOutput(stillImageOutput)

            // use the high resolution photo preset
            captureSession.sessionPreset = AVCaptureSessionPresetPhoto


            // setup camera preview
            cameraLayer = AVCaptureVideoPreviewLayer(session: captureSession)


            if let player = cameraLayer {
                player.videoGravity = AVLayerVideoGravityResizeAspectFill
                self.layer.addSublayer(player)
                player.frame = self.layer.bounds
                player.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight
            }

            // commit and start capturing
            captureSession.commitConfiguration()
            captureSession.startRunning()
        }

        captureSession.commitConfiguration()
    }

    func setFocusWithLensPosition(pos: CFloat) {
        let error: NSErrorPointer = nil
        do {
            try captureDevice!.lockForConfiguration()
        } catch let error1 as NSError {
            error.memory = error1
        }
        captureDevice!.setFocusModeLockedWithLensPosition(pos, completionHandler: nil)
        captureDevice!.unlockForConfiguration()
    }

    // return the camera device for a position
    func cameraDeviceForPosition(position:AVCaptureDevicePosition) -> AVCaptureDevice?
    {
        for device:AnyObject in AVCaptureDevice.devices() {
            if (device.position == position) {
                return device as? AVCaptureDevice;
            }
        }

        return nil
    }

}
Run Code Online (Sandbox Code Playgroud)