UIView

2015-02-12 10 views
8

पर लाइव कैमरा पूर्वावलोकन कैसे जोड़ें I समस्या में भाग लेते हैं, मैं UIView सीमा के भीतर हल करने का प्रयास कर रहा हूं, क्या UIView में कैमरा पूर्वावलोकन जोड़ने का कोई तरीका है? और UIView (बटन, लेबल इत्यादि) के शीर्ष पर अन्य सामग्री जोड़ें?UIView

मैं AVFoundation फ्रेमवर्क का उपयोग करने का प्रयास करता हूं लेकिन स्विफ्ट के लिए पर्याप्त दस्तावेज़ीकरण नहीं है।

उत्तर

23

स्विफ्ट 3 के लिए अद्यतन

आप कुछ इस तरह की कोशिश कर सकते हैं: मैं का प्रयोग कर एक UIView previewView बुलाया कैमरा शुरू करने के लिए और फिर मैं एक नया UIView बुलाया boxView जो जोड़ने

import UIKit 
import AVFoundation 

class ViewController: UIViewController{ 
    var previewView : UIView! 
    var boxView:UIView! 
    let myButton: UIButton = UIButton() 

    //Camera Capture requiered properties 
    var videoDataOutput: AVCaptureVideoDataOutput! 
    var videoDataOutputQueue: DispatchQueue! 
    var previewLayer:AVCaptureVideoPreviewLayer! 
    var captureDevice : AVCaptureDevice! 
    let session = AVCaptureSession() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     previewView = UIView(frame: CGRect(x: 0, 
              y: 0, 
              width: UIScreen.main.bounds.size.width, 
              height: UIScreen.main.bounds.size.height)) 
     previewView.contentMode = UIViewContentMode.scaleAspectFit 
     view.addSubview(previewView) 

     //Add a view on top of the cameras' view 
     boxView = UIView(frame: self.view.frame) 

     myButton.frame = CGRect(x: 0, y: 0, width: 200, height: 40) 
     myButton.backgroundColor = UIColor.red 
     myButton.layer.masksToBounds = true 
     myButton.setTitle("press me", for: UIControlState.normal) 
     myButton.setTitleColor(UIColor.white, for: UIControlState.normal) 
     myButton.layer.cornerRadius = 20.0 
     myButton.layer.position = CGPoint(x: self.view.frame.width/2, y:200) 
     myButton.addTarget(self, action: #selector(self.onClickMyButton(sender:)), for: UIControlEvents.touchUpInside) 

     view.addSubview(boxView) 
     view.addSubview(myButton) 

     self.setupAVCapture() 
    } 

    override var shouldAutorotate: Bool { 
     if (UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft || 
     UIDevice.current.orientation == UIDeviceOrientation.landscapeRight || 
     UIDevice.current.orientation == UIDeviceOrientation.unknown) { 
      return false 
     } 
     else { 
      return true 
     } 
    } 

    func onClickMyButton(sender: UIButton){ 
     print("button pressed") 
    } 
} 


// AVCaptureVideoDataOutputSampleBufferDelegate protocol and related methods 
extension ViewController: AVCaptureVideoDataOutputSampleBufferDelegate{ 
    func setupAVCapture(){ 
     session.sessionPreset = AVCaptureSession.Preset.vga640x480 
     guard let device = AVCaptureDevice 
     .default(AVCaptureDevice.DeviceType.builtInWideAngleCamera, 
       for: .video, 
       position: AVCaptureDevice.Position.back) else { 
          return 
     } 
     captureDevice = device 
     beginSession() 
    } 

    func beginSession(){ 
     var deviceInput: AVCaptureDeviceInput! 

     do { 
      deviceInput = try AVCaptureDeviceInput(device: captureDevice) 
      guard deviceInput != nil else { 
       print("error: cant get deviceInput") 
       return 
      } 

      if self.session.canAddInput(deviceInput){ 
       self.session.addInput(deviceInput) 
      } 

      videoDataOutput = AVCaptureVideoDataOutput() 
      videoDataOutput.alwaysDiscardsLateVideoFrames=true 
      videoDataOutputQueue = DispatchQueue(label: "VideoDataOutputQueue") 
      videoDataOutput.setSampleBufferDelegate(self, queue:self.videoDataOutputQueue) 

      if session.canAddOutput(self.videoDataOutput){ 
       session.addOutput(self.videoDataOutput) 
      } 

      videoDataOutput.connection(with: .video)?.isEnabled = true 

      previewLayer = AVCaptureVideoPreviewLayer(session: self.session) 
      previewLayer.videoGravity = AVLayerVideoGravity.resizeAspect 

      let rootLayer :CALayer = self.previewView.layer 
      rootLayer.masksToBounds=true 
      previewLayer.frame = rootLayer.bounds 
      rootLayer.addSublayer(self.previewLayer) 
      session.startRunning() 
     } catch let error as NSError { 
      deviceInput = nil 
      print("error: \(error.localizedDescription)") 
     } 
    } 

    func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { 
     // do stuff here 
    } 

    // clean up AVCapture 
    func stopCamera(){ 
     session.stopRunning() 
    } 

} 

यहाँ पूर्वावलोकन से ऊपर है देखें। मैं एक UIButton boxView में जोड़ने

महत्वपूर्ण

याद रखें कि आईओएस 10 और बाद में आप पहले के आदेश कैमरे का उपयोग करने की अनुमति के लिए उपयोगकर्ता पूछने के लिए की जरूरत है। आप एक उद्देश्य स्ट्रिंग के साथ अपने एप्लिकेशन की Info.plist एक साथ करने के लिए एक उपयोग कुंजी जोड़कर ऐसा करते हैं, तो आप उपयोग घोषित करने के लिए असफल हो, जब यह पहले पहुंच बनाता है अपने app दुर्घटना होगा क्योंकि।

यहाँ कैमरा पहुंच अनुरोध को दिखाने के लिए एक स्क्रीनशॉट enter image description here

+0

आप कृपया मुझे इस कोड देखें से अधिक बिना दिखा सकते हैं? मैं इसे बेहतर समझूंगा। मैं वास्तव में इसकी प्रशंसा करता हूँ। स्विफ्ट के लिए वास्तव में नया है। क्षमा करें:/ – Xrait

+0

मैंने आपके उत्तर को ऊपर दिया है। यह वही कर रहा है जो मैं चाहता हूं, लेकिन मुझे जाने के लिए अधिक विचार की आवश्यकता है। – Xrait

0

स्विफ्ट 3:

@IBOutlet weak var cameraContainerView:UIView! 

var imagePickers:UIImagePickerController? 

viewDidLoad पर:

override func viewDidLoad() { 

     super.viewDidLoad() 
     addImagePickerToContainerView() 

    } 

विज्ञापन कंटेनर को देखने के लिए घ कैमरा पूर्वावलोकन:

func addImagePickerToContainerView(){ 

     imagePickers = UIImagePickerController() 
     if UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.front) { 
      imagePickers?.delegate = self 
      imagePickers?.sourceType = UIImagePickerControllerSourceType.camera 

      //add as a childviewcontroller 
      addChildViewController(imagePickers!) 

      // Add the child's View as a subview 
      self.cameraContainerView.addSubview((imagePickers?.view)!) 
      imagePickers?.view.frame = cameraContainerView.bounds 
      imagePickers?.allowsEditing = false 
      imagePickers?.showsCameraControls = false 
      imagePickers?.view.autoresizingMask = [.flexibleWidth, .flexibleHeight] 

     } 
    } 

कस्टम बटन कार्रवाई पर:

@IBAction func cameraButtonPressed(_ sender: Any) { 

     if UIImagePickerController.isSourceTypeAvailable(.camera){ 
      imagePickers?.takePicture() 

     } else{ 

      //Camera not available. 
     } 
    } 
+0

फिर इस छवि से कैप्चर छवि पथ कैसे प्राप्त करें Pickers? .take चित्र() इसे अलग दृश्य में दिखाएं –

 संबंधित मुद्दे