斯威夫特:拍照并上传到Parse; NSInternalConsistencyException

时间:2015-03-01 19:02:42

标签: ios swift

在我的应用中,我有一个按钮可以打开相机视图。当我拍照并“使用图片”时,我将该图像保存到变量中。 然后,UIImageNSData转换为UIImageJPEGRepresentation,并以PFFile的身份发送给Parse。

现在,当我在实际设备上运行应用程序时,当我按上传到解析按钮时,我收到此错误:*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not save file data for buildingCam.jpg : (null)'

这是我的Swift代码:

@IBAction func BuildingCamMainPressed(sender: AnyObject) {
    var jpegImage = UIImageJPEGRepresentation(image, 1.0)
    let file = PFFile(name: "buildingCam.jpg", data: jpegImage)

    file.save()

    var buildingCam = PFObject(className: "buildingCam")
    buildingCam["latitude"] = myLocation.last?.coordinate.latitude
    buildingCam["longitude"] = myLocation.last?.coordinate.longitude

    if(BuildingCamText.text.isEmpty){
        println("Geen tekst")
        BuildingCamText.textColor = UIColor.redColor()
        BuildingCamText.text = "Vul iets in"
    } else {
        BuildingCamText.textColor = UIColor.blackColor()
        buildingCam["description"] = BuildingCamText.text
    }
    buildingCam["file"] = file
    buildingCam.save()
}

1 个答案:

答案 0 :(得分:2)

您是否检查了作为发件人传递的内容?

执行此操作的正确方法是采用UIImagePickerControllerDelegate

 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
        let pickedImage:UIImage = info[UIImagePickerControllerOriginalImage] as UIImage
        let scaledImage = scaleImageWith(pickedImage)
        let imageData = UIImagePNGRepresentation(scaledImage)
        let imageFile:PFFile = PFFile(data: imageData)
        PFUser.currentUser().setObject(imageFile, forKey: kParseClassNameProfileImage)
        PFUser.currentUser().saveInBackgroundWithBlock {
            (success: Bool, error: NSError!) -> Void in
            if (success) {
                self.profileImageView.image = scaledImage
            } else {
                self.presentErrorMessage(error)
            }
        }

        picker.dismissViewControllerAnimated(true, completion: nil)
    }

回答OP的要求: 将您的按钮连接到以下内容并确保您也采用UINavigationControllerDelegate

func imagePicker(){
        var imagePicker: UIImagePickerController = UIImagePickerController()
        imagePicker.sourceType = .PhotoLibrary
        imagePicker.delegate = self

        let returnIcon = UIBarButtonItem(image: kNavBarReturnIcon, style: .Plain, target: navigationController, action: "popViewControllerAnimated:")
        returnIcon.tintColor = kToolbarIconColor

        presentViewController(imagePicker, animated: true, completion: nil)
    }