2016-03-04 68 views
1

我的自定義相機並不是一直在工作。有時候它會拍攝一張照片,而其他時候它會默默地崩潰。我的應用程序由3個標籤欄控制器組成。當我從家庭標籤訪問相機時,我可以拍攝一張照片。當我從配置文件選項卡訪問攝像機時,它不會拍攝照片,並會在didPressTakePhoto功能中隨機崩潰。我沒有在選項卡之間傳遞數據,所以它不應該發生。captureStillImageAsynchronouslyFromConnection返回nil錯誤

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 

    //Set current bottom border to photo 
    borderPhoto.backgroundColor = newColor.CGColor 
    borderPhoto.frame = CGRect(x: 0, y: 0 + libraryView.frame.size.height, width: libraryView.frame.size.width, height: 4) 
    photoView.layer.addSublayer(borderPhoto) 

    //self.tabBarController!.tabBar.hidden = true 

    chosenMode = "Photo" 
    libraryView.removeFromSuperview() 
    photoButton.setBackgroundImage(buttonImage, forState: .Normal) 

    if self.isRunning == false{ 
     captureSession = AVCaptureSession() 
     captureSession!.sessionPreset = AVCaptureSessionPresetPhoto 

     let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) 

     var error: NSError? 

     do { 
      input = try AVCaptureDeviceInput(device: backCamera) 
     } catch let error1 as NSError { 
      error = error1 
      input = nil 
     } 

     if error == nil && captureSession!.canAddInput(input) { 
      captureSession!.addInput(input) 

      stillImageOutput = AVCaptureStillImageOutput() 
      stillImageOutput!.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG] 
      if captureSession!.canAddOutput(stillImageOutput) { 
       captureSession!.addOutput(stillImageOutput) 

       previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 
       previewLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill 
       previewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation.Portrait 
       previewView.layer.addSublayer(previewLayer!) 

       captureSession!.startRunning() 
       self.isRunning = true 



      } 
     } 

    } 
    print("Done ViewWillApear") 

} 

@IBAction func didPressTakePhoto(sender: UIButton) { 
    print("Beginning Method") 

    self.previewLayer?.connection.enabled = false 
    if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) { 
     videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait 
     stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {(sampleBuffer, error) in 
      //print(error) 
      if (sampleBuffer != nil) { 
       let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer) 
       let dataProvider = CGDataProviderCreateWithCFData(imageData) 
       let cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, CGColorRenderingIntent.RenderingIntentDefault) 
       var image = UIImage() 

       if UIDevice.currentDevice().orientation == .Portrait{ 
        image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right) 
       }else if UIDevice.currentDevice().orientation == .LandscapeLeft{ 
        image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Up) 
       }else if UIDevice.currentDevice().orientation == .LandscapeRight{ 
        image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Down) 
       } 

       //Crop the image to a square 
       let imageSize: CGSize = image.size 
       let width: CGFloat = imageSize.width 
       let height: CGFloat = imageSize.height 
       if width != height { 
        let newDimension: CGFloat = min(width, height) 
        let widthOffset: CGFloat = (width - newDimension)/2 
        let heightOffset: CGFloat = (height - newDimension)/2 
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(newDimension, newDimension), false, 0.0) 
        image.drawAtPoint(CGPointMake(-widthOffset, -heightOffset), blendMode: .Copy, alpha: 1.0) 
        image = UIGraphicsGetImageFromCurrentImageContext() 
        let imageData: NSData = UIImageJPEGRepresentation(image, 0.25)! 
        UIGraphicsEndImageContext() 
        self.captImage = UIImage(data: imageData)! 
       } 

      } 
      self.performSegueWithIdentifier("fromCustomCamera", sender: self) 
     }) 

    } 


} 
+0

我覺得有些東西是釋放你的對象.. !!! – Simmy

+0

,你可以請詳細說明 – ConnorB

+0

@ConnorB替換該行 stillImageOutput .captureStillImageAsynchronouslyFromConnection(videoConnection,completionHandler:{(sampleBuffer,誤差) 本 stillImageOutput .captureStillImageAsynchronouslyFromConnection(videoConnection,completionHandler:{(sampleBuffer,誤差) 希望這會對你有幫助 –

回答

1

修正了它。我不得不壓縮我的圖像,因爲它們太大而不能記憶。感謝那些幫助過的人。