gpt4 book ai didi

ios - parse 和 swift 的 PFFile 大小问题

转载 作者:行者123 更新时间:2023-11-30 14:17:10 25 4
gpt4 key购买 nike

为什么我用 iPhone 拍摄的一些照片太大而无法上传到 PFFile 进行解析。限制是 10 mgb,我知道这些图片不可能那么大。有些足够小,但大多数还不够。有没有办法优化解析的大小?这是我的代码

//--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)

}

}
}

}

我确实需要能够为我的应用程序拍照,但我从相机拍摄的每张照片都太大了。

最佳答案

你的代码对我来说看起来有点鱼,我会这样做:

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

我为什么要这么做?看来您可以在尝试同时更新位置和其他详细信息时遇到死锁,换句话说,在后台并行运行两次保存命令。

这能解决您的问题吗?也许,parse.com 并不总是按实际情况发送错误

我还添加了图像大小的日志,但您可以在 if 中对其进行转换,并弹出警报 View ,或者如果图像大于 10Mb 则调整图像大小(我认为 iPhone 中不会发生这种情况)

关于ios - parse 和 swift 的 PFFile 大小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30990701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com