首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义相机视图Swift iOS 8 iPhone Xcode 6.1

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

Stack Overflow用户
提问于 2015-02-27 23:08:51
回答 3查看 21.8K关注 0票数 7

我想在我的iPhone inside of View中使用摄像头。我不想使用典型的全屏相机视图,而是我自己的。

例如,我希望在屏幕中间有一个200x200的正方形,并且有一个相机预览。在这个方块下面我想要一个照相的按钮。该怎么做呢?我是迅捷的初学者。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-02-28 01:00:51

您将希望使用AVFoundation框架来允许您在您在故事板中创建的视图中创建自己的AVCaptureSession。这是一个很好的教程,告诉你如何找到摄像头并创建一个捕获会话:

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

本教程使用整个视图作为捕获视图,所以如果您按照他的代码建模,那么相机将有多大。要在屏幕中央创建一个200x200的正方形,您必须在故事板中的视图控制器上绘制一个正方形,将其链接到快速文件中所有代码所在的变量,然后更改底部的部分,

代码语言:javascript
复制
previewLayer?.frame = self.view.layer.frame

转到your200by200View.layer.frame

希望这能有所帮助。如果没有,我可以尝试帮助更多,或者有人可以纠正我。

祝好运!

票数 10
EN

Stack Overflow用户

发布于 2015-10-09 00:57:16

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

代码语言:javascript
复制
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
    }

}
票数 5
EN

Stack Overflow用户

发布于 2015-02-28 04:31:47

下面的示例展示了如何添加cameraOverlayView以在屏幕中央创建一个200x200正方形的查看窗口:

代码语言:javascript
复制
@IBAction func takePhoto(sender: AnyObject) {

    if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
        return
    }

    var imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.Camera;

    //Create camera overlay
    let pickerFrame = CGRectMake(0, UIApplication.sharedApplication().statusBarFrame.size.height, imagePicker.view.bounds.width, imagePicker.view.bounds.height - imagePicker.navigationBar.bounds.size.height - imagePicker.toolbar.bounds.size.height)
    let squareFrame = CGRectMake(pickerFrame.width/2 - 200/2, pickerFrame.height/2 - 200/2, 200, 200)
    UIGraphicsBeginImageContext(pickerFrame.size)

    let context = UIGraphicsGetCurrentContext()
    CGContextSaveGState(context)
    CGContextAddRect(context, CGContextGetClipBoundingBox(context))
    CGContextMoveToPoint(context, squareFrame.origin.x, squareFrame.origin.y)
    CGContextAddLineToPoint(context, squareFrame.origin.x + squareFrame.width, squareFrame.origin.y)
    CGContextAddLineToPoint(context, squareFrame.origin.x + squareFrame.width, squareFrame.origin.y + squareFrame.size.height)
    CGContextAddLineToPoint(context, squareFrame.origin.x, squareFrame.origin.y + squareFrame.size.height)
    CGContextAddLineToPoint(context, squareFrame.origin.x, squareFrame.origin.y)
    CGContextEOClip(context)
    CGContextMoveToPoint(context, pickerFrame.origin.x, pickerFrame.origin.y)
    CGContextSetRGBFillColor(context, 0, 0, 0, 1)
    CGContextFillRect(context, pickerFrame)
    CGContextRestoreGState(context)

    let overlayImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext();

    let overlayView = UIImageView(frame: pickerFrame)
    overlayView.image = overlayImage
    imagePicker.cameraOverlayView = overlayView
    self.presentViewController(imagePicker, animated: true, completion: nil)

}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28768210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档