上传图片以解析ios 9

时间:2015-12-11 05:49:47

标签: ios swift parse-platform

我正在创建一个应用程序,允许用户上传文本和从图书馆或摄像头拍摄的图像,然后上传到解析。我上传文本没有问题,但它只会上传图像,在20次尝试中解析1。有什么想法吗?我已经发布了我的代码。

@IBAction func saveData(sender: AnyObject) {

    var imageText = messageText.text
    var uploadDate = NSDate()
    let formatter = NSDateFormatter()
    formatter.timeStyle = .MediumStyle
    formatter.stringFromDate(uploadDate)

    if messageText.text == nil {
        print("Image not uploaded")
    }else {
        var posts = PFObject(className: "Memento")
        posts["imageText"] = imageText
        posts["uploadTime"] = uploadDate
        posts["uploader"] = PFUser.currentUser()
        posts.saveInBackgroundWithBlock({
            (success: Bool, error: NSError?) -> Void in

            if error == nil {

                var imageData = UIImagePNGRepresentation(self.imagePreview.image!)
                var parseImageFile = PFFile(name: "uploaded_image.png", data: imageData!)
                posts["imageFile"] = parseImageFile
                posts.saveInBackgroundWithBlock({
                    (success: Bool, error: NSError?) -> Void in

                    if error == nil {
                        print("data uploaded")
                        self.performSegueWithIdentifier("saveHome", sender: self)

                    }else {
                        print(error)
                    }
                })
            } else {
                print(error)
            }

        })


    }


}

1 个答案:

答案 0 :(得分:0)

确保将图像文件保存到Parse并将其添加到posts对象。

@IBAction func saveData(sender: AnyObject) {

var imageText = messageText.text
var uploadDate = NSDate()
let formatter = NSDateFormatter()
formatter.timeStyle = .MediumStyle
formatter.stringFromDate(uploadDate)

if messageText.text == nil {
    print("Image not uploaded")
} else {

    // Save the image
    //var imageData = UIImagePNGRepresentation(self.imagePreview.image!)
    //var parseImageFile = PFFile(name: "uploaded_image.png", data: imageData!)
    var parseImageFile = PFFile(name: "uploaded_image.jpg", data: UIImageJPEGRepresentation(self.imagePreview.image!, 0.6)!)!
    parseImageFile.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
        if (success) {
            println("picture saved")
        } else {
            // Log details of the failure
            println("Picture Save Error: \(error!) \(error!.userInfo)")
        }
    }

    // Then save your posts object
    var posts = PFObject(className: "Memento")
    posts["imageText"] = imageText
    posts["uploadTime"] = uploadDate
    posts["uploader"] = PFUser.currentUser()
    posts["imageFile"] = parseImageFile
    posts.saveInBackgroundWithBlock({
        (success: Bool, error: NSError?) -> Void in
        if error == nil {
            print("data uploaded")
            self.performSegueWithIdentifier("saveHome", sender: self)
        } else {
            // Log details of the failure
            println("Picture Save Error: \(error!) \(error!.userInfo)")
        }
    })
}
}