使用 swift 3 在 iOS 中存储捕获的图像

Posted

技术标签:

【中文标题】使用 swift 3 在 iOS 中存储捕获的图像【英文标题】:Store captured image in iOS using swift 3 【发布时间】:2016-12-18 14:20:38 【问题描述】:

我正在处理这项任务,我们必须构建自定义相机功能并将图像存储到手机图库中。所以我几乎在相机预览之前做了,但我还不确定如何使用 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


【问题讨论】:

【参考方案1】:

拍照真的很简单,看看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
    

【讨论】:

大部分是简化的方式,感谢您试用并提供反馈@BogdanFarca 除了使用 ImagePicker 控制器之外,我希望实现自定义相机,您是否可以提供任何可用资源@BogdanFarca @SharkesMonken:这里有一个关于如何实现自定义相机的很好的解释:github.com/codepath/ios_guides/wiki/… 感谢您抽出宝贵时间提供更多详细信息@bogdan 如何使用名称标识符保存图像

以上是关于使用 swift 3 在 iOS 中存储捕获的图像的主要内容,如果未能解决你的问题,请参考以下文章

如何在 iOS swift 3 中创建单独的文件夹

是否可以在 Swift 中捕获的图像中添加自己的元数据

IOS - 如何在 Swift 3 中从 JSON 中捕获动态数组维度

将图像从 ios swift 3 上传到 jsp Web 服务

在 Swift 中捕获原始图像类型

使用自动增量 Swift 3 在 Firebase 数据库中存储帖子(图像、标题)