Ios captureStillImageAsynchronouslyFromConnection返回零错误

Ios captureStillImageAsynchronouslyFromConnection返回零错误,ios,swift,avcapturesession,Ios,Swift,Avcapturesession,我的定制相机不是一直在工作。有时它会拍照,有时它会无声地崩溃。我的应用程序由3个选项卡和一个选项卡栏控制器组成。当我从“主页”选项卡访问相机时,我可以很好地拍照。当我从“个人资料”选项卡访问相机时,它不会拍照,并且会在didPressTakePhoto功能的任意位置崩溃。我没有在选项卡之间传递数据,所以它不应该发生 override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) //Se

我的定制相机不是一直在工作。有时它会拍照,有时它会无声地崩溃。我的应用程序由3个选项卡和一个选项卡栏控制器组成。当我从“主页”选项卡访问相机时,我可以很好地拍照。当我从“个人资料”选项卡访问相机时,它不会拍照,并且会在didPressTakePhoto功能的任意位置崩溃。我没有在选项卡之间传递数据,所以它不应该发生

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)
        })

    }


}

修好了。我不得不压缩我的图像,因为它们太大了,无法存储。感谢那些帮助过你的人。

我想有什么事情正在释放你的物品。。!!!你能帮我吗elaborate@ConnorB将中的此行stillImageOutput?.CaptureSillImageAsynchronousLyfromConnection(videoConnection,completionHandler:{(sampleBuffer,错误)替换为此stillImageOutput!。CaptureSillImageAsynchronousLyfromConnection(videoConnection,completionHandler:{(sampleBuffer,错误)希望这会对您有所帮助。现在确实有效。完成处理程序中的错误返回nil,然后应用程序在几行之后崩溃samplebuffer和错误本身可能都是nil。您不能只打印(错误),因为它是隐式展开的。