我试图使用这个iOS swift代码拍摄图像,当我尝试捕获时,我看到以下内容

时间:2015-02-15 23:33:09

标签: ios swift avcapturesession

我试图使用这个iOS swift代码捕获图像,任何帮助将不胜感激 错误 'NSInvalidArgumentException',原因:'*** + [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:] - NULL样本缓冲区。'

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

0 个答案:

没有答案