ios - 如何将图像保存到 Parse 的主要用户类?

标签 ios objective-c xcode parse-platform

我有以下方法:

- (void) savePhotoToUser {

    NSData *imageData = UIImageJPEGRepresentation(_imgView.image, 1.0);
    PFFile *imageFile = [PFFile fileWithName:@"img.png" data:imageData];

    [imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {


            //THIS IS WHERE I NEED HELP
            //
            PFObject *userPhoto = [PFObject objectWithClassName:@"User"];
            //
            //

            [userPhoto setObject:imageFile forKey:@"profilePicture"];

            [userPhoto saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (!error) {
                    NSLog(@"Saved %@ for user %@", imageFile, userPhoto);

                    //Push ViewController

                }
                else{
                    NSLog(@"Error: %@ %@", error, [error userInfo]);
                }
            }];
        }
        else{
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];
}

我正在尝试将 PFObject 保存到 Parse 中的主用户类。我有一个带有 profilePicture 标签的列,但是当我运行该方法时,我得到一个名为 User 的新分类(除了现有的分类之外)并且图像被拖放在那里。我正在显示我的 Parse 实例的图像以及用于保存图像的类和预期对象。

任何帮助将不胜感激! :)

Parse DB Image

最佳答案

这是有效的: 需要做的(@Vidhyanand900 在正确的轨道上,但不是 100%)是将图像保存在后台,然后 setObject 将图像设置为当前用户。

NSData *imageData = UIImageJPEGRepresentation(_imgView.image, 1.0);
PFFile *imageFile = [PFFile fileWithName:@"img.png" data:imageData];
[imageFile saveInBackground];

PFUser *user = [PFUser currentUser];
[user setObject:imageFile forKey:@"profilePicture"];
[user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (!error) {

//...REST OF CODE...

答案在这里找到:https://parse.com/questions/update-table-user-with-a-profile-image


这是 Swift 中的答案:

let imageData: NSData = UIImageJPEGRepresentation(image, 1.0)
let imageFile: PFFile = PFFile(name:"image.jpg", data:imageData)
imageFile.save()

let user = PFUser.currentUser()
user.setObject(imageFile, forKey: "profilePicture")
user.saveInBackgroundWithBlock {
            (success: Bool!, error: NSError!) -> Void in
            if (success != nil) {

                println("saving")
}
}

关于ios - 如何将图像保存到 Parse 的主要用户类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24133598/

相关文章:

ios - 为什么我不能在 ViewController 中创建数组?

ios - 动态设置 UICollectionViewCell 的大小

objective-c - 显示来自 NSDocumentDirectory 的照片

ios - UiTextView 更改字体大小不起作用

Xcode - 在新窗口中打开当前文件(真正的挑战!)

iphone - 应用程序不在后台模式下运行

ios - GMSMarker 未与用户对齐

ios - 没有键映射的 RestKit 关系连接到特定的核心数据对象

Objective-c 如何从终端应用程序制作 GUI 窗口

ios - 从位置事件正确地将 iOS 应用程序启动到后台