2015-10-29 72 views
0

我已經搜索了很長時間,現在沒有任何東西可以用於我。我能夠將用戶的用戶名,電子郵件,密碼和樣本位置上傳到Parse上的「用戶」類。但是我需要上傳一張圖片給用戶的佔位符配置文件圖像。我在Xcode的資產中有圖像(ProfileImagePlaceHolder.png)使用Swift 2.0添加圖像進行分析使用Swift 2.0

如何將此圖像上傳到用戶的個人資料圖片進行分析?

@IBAction func registerTapped(sender: UIButton) 
{ 
    print("Register Page - Register Tapped") 

    if txtEmailAddress.text == "" || txtPassword.text == "" || txtPasswordConfirmation.text == "" 

    { 
     print("Registration fields are not complete.") 

     REGISTER_INCOMPLETE_FIELD.show() 

    } 
    else if txtPassword.text != txtPasswordConfirmation.text 
    { 

     print("Password Confirmation does not match with Password.") 

     PASSWORD_CONFIRMATION_DOESNT_MATCH.show() 

    } 
    else 
    { 
     let user = PFUser() 

     user.username = txtEmailAddress.text 
     user.password = txtPassword.text 
     user.email = txtEmailAddress.text 
     user["location"] = PFGeoPoint(latitude: 41, longitude: 29) 
     user.signUpInBackgroundWithBlock { 
      (succeeded: Bool, error: NSError?) -> Void in 
      if error != nil { 

       print("There is a problem.") 

       SOMETHING_IS_WRONG.show() 

      } else { 
       // Hooray! Let them use the app now. 

       print("Successfully registered.") 

       SUCCESS_REGISTER.show() 

       self.performSegueWithIdentifier(SEGUE_REGISTERED, sender: self) 

       // 
      } 
     } 
    } 

} 
+0

這個問題你檢查我下面的答案嗎? – Vijay

回答

1

解析對這個確切的主題是iOS教程:https://www.parse.com/docs/ios/guide#files

下面是代碼保存文件來分析。你可以把它和你的PFObject關聯起來。

// Convert to JPEG with 50% quality 
NSData* data = UIImageJPEGRepresentation(imageView.image, 0.5f); 
PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:data]; 

// Save the image to Parse 

[imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
    if (!error) { 
     // The image has now been uploaded to Parse. Associate it with a new object 
     PFObject* newPhotoObject = [PFObject objectWithClassName:@"PhotoObject"]; 
     [newPhotoObject setObject:imageFile forKey:@"image"]; 
     [newPhotoObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
      if (!error) { 
       NSLog(@"Saved"); 
      } 
      else{ 
       // Error 
       NSLog(@"Error: %@ %@", error, [error userInfo]); 
      } 
     }]; 
    } 
}]; 

在這裏,斯威夫特

let imageData = UIImagePNGRepresentation(image) 
let imageFile = PFFile(name:"image.png", data:imageData) 

var userPhoto = PFObject(className:"UserPhoto") 
userPhoto["imageName"] = "My trip to Hawaii!" 
userPhoto["imageFile"] = imageFile 
userPhoto.saveInBackground() 
+0

我無法將此代碼配置到我的應用程序。當我寫, 讓imageFile = PFFile(名稱:「profileplaceholder.png」,數據:imageData) 它說「使用未解析的標識符'imageData」,如果我不能配置它,它將圖像保存爲字符串不作爲文件。 – melkaya

+0

@melkaya您是否閱讀過Parse指南? – Vijay

+0

是的,我做了,但我找不到解決方案。我下載了一個名爲swiftagram的應用程序並閱讀它的代碼,然後我找到了解決方案,但感謝您的幫助。 – melkaya

0

我已經解決了與

 let imageToBeUploaded = self.imgProfileImage.image 
     let imageData = UIImagePNGRepresentation(imageToBeUploaded!) 
     let imageFile = PFFile(name:"profileplaceholder.png", data:imageData!) 
     let user = PFUser() 

     user.username = txtEmailAddress.text 
     user.password = txtPassword.text 
     user.email = txtEmailAddress.text 
     user["location"] = PFGeoPoint(latitude: 41, longitude: 29) 
     user["image"] = imageFile