ios - 上传图像解析ios 9

标签 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)
            }

        })


    }


}

最佳答案

确保将图像文件保存到 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)")
        }
    })
}
}

关于ios - 上传图像解析ios 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34216927/

相关文章:

c++ - 使用 Apple Accelerate Framework 的希尔伯特变换(分析信号)?

ios - 仅在一个部分显示表格 View 页脚

ios - 任何人在 AppDelegate func handleWatchKitExtensionRequest 中调用 Parse 查询时遇到问题并且没有获得查询结果

ios - Parse & Swift- 没有匹配查询的结果

ios - CoreData持久化到磁盘时一一选择关联中的所有行

ios - 在应用程序内购买

ios - 在最新版本的 XCode 中,为什么即使重置了设置,模拟器也无法运行?

swift - 为什么我的解析查询在 AppDelegate 中成功,但在 ViewController 中失败?

swift - UITapGestureRecognizer 无法直接连接到助理编辑器?

core-data - 核心数据 : Extend Predicate to fetch related entities with special Attribute-Value