我正在创建一个应用程序,允许用户上传文本和图像,无论是从图书馆或从相机和上传解析。我得到的文本上传没有问题,但它只会上传图像解析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)
}
})
}
}发布于 2015-12-11 15:02:45
确保将图像文件保存到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)")
}
})
}
}https://stackoverflow.com/questions/34216927
复制相似问题