Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正在上载图像以解析ios 9_Ios_Swift_Parse Platform - Fatal编程技术网

正在上载图像以解析ios 9

正在上载图像以解析ios 9,ios,swift,parse-platform,Ios,Swift,Parse Platform,我正在创建一个应用程序,允许用户上传从图书馆或相机拍摄的文本和图像,并上传到解析。我得到的文本上传没有问题,但它只会上传图像解析1在20次尝试。有什么想法吗?我已经发布了我的代码 @IBAction func saveData(sender: AnyObject) { var imageText = messageText.text var uploadDate = NSDate() let formatter = NSDateFormatter() forma

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

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

        })


    }


}

确保保存要解析的图像文件,并将其添加到
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)")
        }
    })
}
}

如果有错误,您会得到什么?为什么要将
posts
对象保存两次?为什么不设置图像并保存一次呢?我没有必要出错。它只会上传大约1/20的图片来解析。谢谢你的修改Russell。但是,现在我遇到了一个异常,它说“无法使用数据创建PFFile:数据大于10MB。”正如错误所说,正在上载的图片太大。您可能希望在上传照片之前对其进行压缩。我本来把它漏掉了,因为你以前没有。查看更新后的answerI,我还将其转换为JPEG格式,用于普通照片。0.6是压缩系数