【问题标题】:Adding an Image to Parse Using Swift 2.0使用 Swift 2.0 添加图像进行解析
【发布时间】:2015-10-29 11:08:56
【问题描述】:

我已经搜索了很长时间,但没有任何结果适合我。我能够将用户的用户名、电子邮件、密码和示例位置上传到 Parse 上的“用户”类。但我需要将图像上传到 Parse 以获取用户的占位符个人资料图像。我在 Xcode (ProfileImagePlaceHolder.png) 的资产中有图像

如何将此图片上传到 Parse 作为用户的个人资料图片?

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

                //
            }
        }
    }

}

【问题讨论】:

  • 你检查我下面的答案了吗?

标签: ios swift parse-platform swift2


【解决方案1】:

Parse 有一个关于这个确切主题的 iOS 教程:https://www.parse.com/docs/ios/guide#files

这是将文件保存到 Parse 的代码。您可以将其与您的 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]);
            }
        }];
    }
}];

在 Swift 中

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

【讨论】:

  • 我无法将此代码配置到我的应用程序中。当我写的时候, let imageFile = PFFile(name:"profileplaceholder.png", data:imageData) 它说“使用未解析的标识符'imageData”,如果我无法配置它,它将图像保存为字符串而不是文件。
  • 是的,但我找不到解决方案。我下载了一个名为 swiftagram 的应用程序并阅读了它的代码,然后我找到了解决方案,但感谢您的帮助。
  • @melkaya 如果您的解决方案与我的不同,请将其发布为该问题的答案。这可能对面临同样问题的人有所帮助。如果两者相似,请接受我的回答。
【解决方案2】:

我已经解决了这个问题

        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

【讨论】:

    猜你喜欢
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多