2017-08-06 63 views
0

我新的iOS开发,我试图按照下面的教程来学习如何捕捉图像,然后将其保存为我的目的服务器上:UIButton的捕捉到的图像没有响应

https://medium.com/@rizwanm/swift-camera-part-2-c6de440a9404

该教程是伟大的,我只是初始化相机,并尝试拍摄图像(不做QR部分)。 不幸的是,当我点击'捕捉'按钮时,图像没有被拍摄或保存在我的画廊,我无法理解为什么。我使用Xcode 8.3,并在iPhone上运行iOS 10.3。

我已将按钮连接到视图控制器,并在函数onTapTakePhoto中调用它,如下面的代码所示。请告知为什么会发生这种情况。

import UIKit 
import AVFoundation 

class ViewController: UIViewController { 
    @IBOutlet weak var previewView: UIView! 
    @IBOutlet weak var CaptureButton: UIButton! 

    //helps transfer data between one or more input device like camera 
    var captureSession: AVCaptureSession? 
    //instance variable 
    var capturePhotoOutput: AVCapturePhotoOutput? //capturePhotoOutput will help us snap a live photo 

    //helps render the camera view finder in ViewController 
    var videoPreviewLayer: AVCaptureVideoPreviewLayer? 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     CaptureButton.layer.cornerRadius = CaptureButton.frame.size.width/2 
     CaptureButton.clipsToBounds = true 

     //set up camera here 
     let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) 

     // get an instance of the AVCAptureDevicenput 
     // serves as a middle man to attach input device to capture device 
     // chance the input device unavailable so wrap in do catch 

     do { 
      // Get an instance of the AVCaptureDeviceInput class using the previous deivce object 
      let input = try AVCaptureDeviceInput(device: captureDevice) 
      // Initialize the captureSession object 
      captureSession = AVCaptureSession() 

      // Set the input devcie on the capture session 
      captureSession?.addInput(input) 

      //set up preview view to see live feed 
      videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 
      videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill 
      videoPreviewLayer?.frame = view.layer.bounds 
      previewView.layer.addSublayer(videoPreviewLayer!) 

      // finally start the capture session 
      captureSession?.startRunning() 

      // get an instance of AVCapturePhotoOutput class 
      capturePhotoOutput = AVCapturePhotoOutput() 
      capturePhotoOutput?.isHighResolutionCaptureEnabled = true 
      //set the outut on the capture session 
      captureSession?.addOutput(capturePhotoOutput) 

     } catch { 
      print (error) 
      return 
     } 
    } 

    @IBAction func onTapTakePhoto(_ sender: UIButton) { 
     // Make sure capturePhotoOutput is valid 
     guard let capturePhotoOutput = self.capturePhotoOutput else { return } 

     // Get an instance of AVCapturePhotoSettings class 
     let photoSettings = AVCapturePhotoSettings() 

     // Set photo settings for our need 
     photoSettings.isAutoStillImageStabilizationEnabled = true 
     photoSettings.isHighResolutionPhotoEnabled = true 
     photoSettings.flashMode = .auto 

     // Call capturePhoto method by passing our photo settings and a delegate implementing AVCapturePhotoCaptureDelegate 
     capturePhotoOutput.capturePhoto(with: photoSettings, delegate: self) 
    } 
} 

// to get the captured image 
extension ViewController : AVCapturePhotoCaptureDelegate { 
    func capture(_ captureOutput: AVCapturePhotoOutput, 
       didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, 
       previewPhotoSampleBuffer: CMSampleBuffer?, 
       resolvedSettings: AVCaptureResolvedPhotoSettings, 
       bracketSettings: AVCaptureBracketedStillImageSettings?, 
       error: Error?) { 
     // Make sure we get some photo sample buffer 
     guard error == nil, 
      let photoSampleBuffer = photoSampleBuffer else { 
       print("Error capturing photo: \(String(describing: error))") 
       return 
     } 

     // Convert photo same buffer to a jpeg image data by using AVCapturePhotoOutput 
     guard let imageData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else { 
      return 
     } 

     // Initialise an UIImage with our image data 
     let capturedImage = UIImage.init(data: imageData , scale: 1.0) 
     if let image = capturedImage { 
      // Save our captured image to photos album 
      UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) 
     } 
    } 
} 

回答

0

你应该检查的一件事是,如果你已经在info.plist文件中添加权限来访问摄像机。

你必须做的关键

隐私键值项 - 图片库使用情况说明

(如果您使用的是图库)

隐私 - 相机使用描述on

使用相机本身。

有关更多详细信息,请参阅this

如果不起作用,请将调试器放在按钮的单击操作上,并检查代码流。如果按下按钮后调试器没有被击中,则按钮动作插座可能会出现问题,请尝试重新制作插座动作。

0

谢谢你的答案:)

的问题是我的操作按钮丢失连接来自故事板的代码。感觉像回想起这样一个愚蠢的错误

0
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 

     if (info[UIImagePickerControllerOriginalImage] as? UIImage) != nil { 
      } 
     dismiss(animated: true, completion: 
      { 
       // write your set image code for UIButton is here. 
      }) 
    }