我正在尝试使用iOS swift代码拍摄一张图像,当我尝试拍摄时,我看到了以下内容

我正在尝试使用iOS swift代码拍摄一张图像,当我尝试拍摄时,我看到了以下内容,ios,swift,avcapturesession,Ios,Swift,Avcapturesession,我正在尝试使用此iOS swift代码捕获图像,如有任何帮助,将不胜感激 错误 “NSInvalidArgumentException”,原因:“***+[AvCaptureSillImageOutput jpegStillImageNSDataRepresentation:]-空样本缓冲区。” @IBAction func capture(sender: AnyObject) { if let stillOutput = self.stillImageOutput as AVCaptu

我正在尝试使用此iOS swift代码捕获图像,如有任何帮助,将不胜感激
错误 “NSInvalidArgumentException”,原因:“***+[AvCaptureSillImageOutput jpegStillImageNSDataRepresentation:]-空样本缓冲区。”

@IBAction func capture(sender: AnyObject) {
    if let stillOutput = self.stillImageOutput as AVCaptureStillImageOutput? {
        // we do this on another thread so that we don't hang the UI
        println("image1")
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
            //find the video connection
            var videoConnection : AVCaptureConnection?
            for connecton in stillOutput.connections {
                //find a matching input port
                println("image2")

                for port in connecton.inputPorts!{
                    println("image3")
                    if port.mediaType == AVMediaTypeVideo {
                        videoConnection = connecton as? AVCaptureConnection
                        break //for port
                    }
                }

                if videoConnection  != nil {
                    println("image4")
                    println(videoConnection)
                    break// for connections
                }
            }
            if videoConnection  != nil {
                println("image5")
                stillOutput.captureStillImageAsynchronouslyFromConnection(videoConnection){
                    (imageSampleBuffer : CMSampleBuffer!, error:  NSError!) in
                    //if(error != nil){
                    println("error")
                    println(error)
                    //}
                    println("imageBuffer")
                    println(imageSampleBuffer)
                    let imageDataJpeg = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageSampleBuffer)
                    var pickedImage: UIImage = UIImage(data: imageDataJpeg)!

                   // var pickedImage: UIImage = UIImage(named: "hand.jpg")!
                    println("pic")
                    var handFlag = NSUserDefaults.standardUserDefaults().objectForKey(kleftHand) as NSString
                    var userName =  NSUserDefaults.standardUserDefaults().objectForKey(KuserNameKey) as NSString




                    if (handFlag == "true"){
                        var LeftObject =  PFObject(className: "LeftHandObject")
                        let imageDataLeft = UIImagePNGRepresentation(pickedImage)
                        let imageFileLeft = PFFile(name: "leftImage.png", data: imageDataLeft)
                        LeftObject["userName"] = userName
                        LeftObject["leftImage"] =  imageFileLeft
                        LeftObject["goalObjId"] = NSUserDefaults.standardUserDefaults().objectForKey(kCurrentGoalObjectId) as NSString
                        LeftObject.saveInBackgroundWithBlock{
                            (successObjectLeft: Bool!, errorObjectLeft: NSError!) -> Void in
                            if(errorObjectLeft == nil){
                                println("SuccessLeft")
                            }
                            else{
                               println("ErrorLeft")
                            }

                        }

                    }
                    else{
                        var RightObject =  PFObject(className: "RightHandObject")
                        let imageDataRight = UIImagePNGRepresentation(pickedImage)
                        let imageFileRight = PFFile(name: "rightImage.png", data: imageDataRight)
                        RightObject["userName"] = userName
                        RightObject["rightImage"] =  imageFileRight
                        RightObject["goalObjId"] = NSUserDefaults.standardUserDefaults().objectForKey(kCurrentGoalObjectId) as NSString
                        RightObject.saveInBackgroundWithBlock{
                            (successObjectRight: Bool!, errorObjectRight: NSError!) -> Void in
                            if(errorObjectRight == nil){
                                 println("SuccessRight")
                            }
                            else{
                                println("SuccessLeft")
                            }
                        }
                    }
                  }
                }
                self.captureSession.stopRunning()
            }
        }

    println("take photo pressed")
}

如此深的嵌套表明该方法应该分解为几个子方法。深度嵌套使得很难遵循逻辑。给每个子方法一个好的描述性名称对于编写代码有很大帮助。深度嵌套还表明正在执行许多不同的操作,这违反了单一责任原则。让imageDataJpeg=AVCaptureSillImageOutput.jpegStillImageNSDataRepresentation(imageSampleBuffer)var pickeImage:UIImage=UIImage(data:imageDataJpeg)!imageSampleBuffer为零,我就是不知道我哪里出错了!