为什么我在iphone上拍的一些照片太大而无法上传到PFFile进行解析。限制是10毫克,我知道这些照片不会那么大。有些是小的,但大多数不是。无论如何都要优化解析的大小?这是我的代码
//--CONTROLS HOW TO PICK THE IMAGE WHEN THIS BUTTON IS CLICKED--\\
@IBAction func chooseImage(sender: AnyObject) {
var image = UIImagePickerController()
image.delegate = self
image.navigationBar.tintColor = UIColor.whiteColor()
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
}
@IBAction func postImage(sender: AnyObject) {
self.view.endEditing(true)
var error = ""
if photoSelected == false{
error="Please select an image to post."
}
else if contactText.text == ""{
error = "Please enter an email or phone number."
}
else if nameOfPost.text == ""{
error="Please enter the name of the post."
}
else if descriptionOfPost.text == ""{
error="Please enter a description about your posts."
}
if error != ""{
displayAlert("Oops! There was an error with posting.", error: error)
}
else{
activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 125, 125))
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
activityIndicator.backgroundColor = UIColor(red: (158/255.0), green: (40/255.0), blue: (52/255.0), alpha: 1)
activityIndicator.layer.cornerRadius = 10
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
//--MAIN CODE TO POST--\\
var post = PFObject(className: "Posts")
post["Contact"] = contactText.text
post["Name"] = nameOfPost.text
post["Description"] = descriptionOfPost.text
let imageData = UIImagePNGRepresentation(self.imageToPost.image)
let imageFile = PFFile(name: "image.png", data: imageData)
post["imageFile"] = imageFile
post["createdBy"] = PFUser.currentUser()?.objectId
//--GETTING LOCATION OF THE POST--\\
PFGeoPoint.geoPointForCurrentLocationInBackground {
(geoPoint: PFGeoPoint?, error: NSError?) -> Void in
if error == nil {
post.setValue(geoPoint, forKey: "Location")
post.saveInBackground()
println("found location")
}
}
//--SAVING THE Post HERE--\\
post.saveInBackgroundWithBlock{(success:Bool, error:NSError?)-> Void in
self.activityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
if success == false{
self.activityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
self.displayAlert("Sorry! We could not post.", error: "Please try again later.")
}
//--NO ERRORS SO NOW WE CAN SAVE AND POST==\\
else{
self.performSegueWithIdentifier("backToFeed", sender: self)
}
}
}
}
我真的需要能够为我的应用拍照,但是我从相机拍摄的每张照片都太大了。
答案 0 :(得分:0)
你的代码看起来有点像鱼,我会这样做:
var post:PFObject
func prepareToSavePost(){
post = PFObject(className: "Posts")
post["Contact"] = contactText.text
post["Name"] = nameOfPost.text
post["Description"] = descriptionOfPost.text
let imageData = UIImagePNGRepresentation(self.imageToPost.image)
let imageFile = PFFile(name: "image.png", data: imageData)
post["imageFile"] = imageFile
post["createdBy"] = PFUser.currentUser()?.objectId
//Get bytes size of image
var imageSize = Float(imageData.length)
//Transform into Megabytes
imageSize = imageSize/(1024*1024)
println("Image size is \(imageSize)Mb")
PFGeoPoint.geoPointForCurrentLocationInBackground {
(geoPoint: PFGeoPoint?, error: NSError?) -> Void in
if error == nil {
self.post.setValue(geoPoint, forKey: "Location")
println("found location")
savePost()
}
}
}
func savePost(){
post.saveInBackgroundWithBlock{
(success:Bool, error:NSError?)-> Void in
self.activityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
if success == false{
self.activityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
self.displayAlert("Sorry! We could not post.", error: "Please try again later.")
}
//--NO ERRORS SO NOW WE CAN SAVE AND POST==\\
else{
self.performSegueWithIdentifier("backToFeed", sender: self)
}
}
}
}
为什么我会这样做?看起来你可以在死锁中试图同时更新位置和其他细节,换句话说,在后台运行save命令并行两次。
这会解决您的问题吗?也许,parse.com并不总是发送错误,因为它确实是
我还为图像大小添加了一个日志,但您可以在if中进行转换并弹出警报视图,或者如果图像大于10Mb则调整图像大小(我认为iPhone中不会发生这种情况)< / p>