ios - 使用Swift 3将捕获的图像存储在iOS中

标签 ios swift3 ios-camera

我正在完成这项任务,我们必须构建自定义摄像头功能并将图像存储到电话库中。因此,我几乎在相机预览之前就做了,但是我不确定如何使用swift 3捕获和存储图像。

这是源代码:

var captureSession = AVCaptureSession()
var sessionOutput = AVCapturePhotoOutput()
var previewLayer = AVCaptureVideoPreviewLayer()






override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}


override func viewWillAppear(_ animated: Bool) {

    super.viewWillAppear(animated)
    self.navigationController?.setNavigationBarHidden(true, animated: false)

    let deviceSession = AVCaptureDeviceDiscoverySession(deviceTypes: [.builtInWideAngleCamera, .builtInDuoCamera,.builtInTelephotoCamera], mediaType: AVMediaTypeVideo, position: .unspecified)

    for device in (deviceSession?.devices)! {

        if device.position == AVCaptureDevicePosition.back {

            do {

                let input = try AVCaptureDeviceInput.init(device: device)

                if captureSession.canAddInput(input) {

                    captureSession.addInput(input)

                    if captureSession.canAddOutput(sessionOutput) {

                        captureSession.addOutput(sessionOutput)
                        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                        previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
                        previewLayer.connection.videoOrientation = .portrait

                        cameraLayerView.layer.addSublayer(previewLayer)
                        cameraLayerView.addSubview(topControllerView)
                        cameraLayerView.addSubview(bottomControllerView)


                        previewLayer.position = CGPoint(x: self.cameraLayerView.frame.width / 2, y: self.cameraLayerView.frame.height / 2)
                        previewLayer.bounds = cameraLayerView.frame

                        captureSession.startRunning()

                    }
                }
            } catch let avError {
                print(avError)
            }
        }
    }


}

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



override func viewWillDisappear(_ animated: Bool) {

    self.navigationController?.setNavigationBarHidden(false, animated:   false)
}



@IBAction func galleryAction(_ sender: UIButton) {

  // This part shows the thumbnail of a current image been taken
}



@IBAction func captureAction(_ sender: UIButton) {

    // This is the part i have capture the image

}

最佳答案

拍照非常简单,看看here
呈现图像选择器:

func takePhoto(sender: UIButton) {
    imagePicker =  UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = .Camera

    presentViewController(imagePicker, animated: true, completion: nil)
}

获取拍摄/选择的图像:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    imagePicker.dismissViewControllerAnimated(true, completion: nil)
    imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}

现在,可以像解释here一样将UIImage保存到相册中。
以下调用会将照片保存到图库并调用完成处理程序:
UIImageWriteToSavedPhotosAlbum(yourImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)

处理程序类似于:
func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
    if let error = error {
        // error
    } else {
        // no error
    }
}

关于ios - 使用Swift 3将捕获的图像存储在iOS中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41209221/

相关文章:

ios - Swift 3 多个手势识别器 SWRevealController

ios - 从相机进行人脸检测 iOS

swift - 在 Swift 中采用 CollectionType(集合)

ios - 从子类在 Swift 中具有泛型的基类转换子类

iphone - 创建 Tableview 的自定义背景的正确方法

ios - 当元素被激活时如何获取indexpath.row?

ios - 使 UIPickerView 依赖于选定的行(Swift)

ios - 在 iOS 中使用相机的对象的高度和宽度

ios - 确定 ios 相机纵横比

ios - 生成带有图像的 UIView