2015-06-22 36 views
0

爲什麼我在我的iPhone上拍攝的一些圖片太大而無法作爲PFFile進行解析。限制是10毫克,我知道這些照片不能那麼大。有些很小,但大多數不是。無論如何要優化分析的大小?這裏是我的代碼分析和快速PFFile大小問題

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

您可以在保存到Parse之前調整圖像大小。 –

+0

我會如何去做這件事。這是用戶驅動的 – user2967062

+0

https://www.google.com/search?client=safari&rls=zh-CN&q=resize+uiimage+ios&ie=UTF-8&oe=UTF-8#safe=off&q=resize+uiimage+ios+site:stackoverflow .com –

回答

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

我爲什麼會做這樣的?看起來,你可以運行在一個死鎖,試圖同時更新位置和其他細節,換句話說,並行地在後臺運行保存命令兩次。

這會解決您的問題嗎?也許,parse.com並不總是發送錯誤,因爲它確實是

我還爲圖像大小添加了一個日誌,但是您可以將它轉換爲if並彈出警報視圖或調整圖像的大小,如果它更大比10Mb(我不認爲會發生在iPhone上)

+0

它說pffile沒有長度的成員..這是在這行中找到var imageSize = Float(imageFile.length) – user2967062

+0

@ user2967062對不起,它應該是imageData不是imageFile,我已經更新了答案 – Icaro

+0

沒問題。它還說了一些關於不能在另一個本地函數中使用本地函數的問題 – user2967062