检索附加到 currentUser / Parse 的照片

Posted

技术标签:

【中文标题】检索附加到 currentUser / Parse 的照片【英文标题】:Retrieve photo attached to currentUser / Parse 【发布时间】:2015-06-11 20:09:04 【问题描述】:

我在检索附加到 currentUser 的图像时遇到问题。 这是我用来保存要解析的信息的代码。

-(IBAction)saveButtonAction:(id)sender 

PFUser *currentUserSave = [PFUser currentUser];

userBiostring = userBio.text;
genderFieldString = genderField.text;
ageFieldString = ageField.text;

profilePictureData = UIImageJPEGRepresentation(profilePicture.image, .05f);
PFFile *userProfilePictureFileContainer = [PFFile fileWithData:profilePictureData];

currentUserSave[@"userBioParse"] = userBioString;
currentUserSave[@"userGenderParse"] = genderFieldString;
currentUserSave[@"ageFieldParse"] = ageFieldString;
[currentUserSave setObject:userProfilePictureFileContainer forKey:@"userPictureParse"];
userImageData = UIImageJPEGRepresentation(profilePicture.image, 0.5);
userProfileImageFile = [PFFile fileWithData:userImageData];
[currentUserSave setObject:userProfileImageFile forKey:@"userPictureParse"];



[[PFUser currentUser] saveInBackground];

基本上,现在我正在尝试回调信息以加载用户个人资料。 这是我到目前为止所拥有的(不多)。

PFQuery *queryUser = [PFUser query];
[queryUser whereKey:@"username" equalTo:[[PFUser currentUser]username]];
[queryUser getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error)


    PFFile *theImage = [object objectForKey:[PFUser currentUser][@"userPictrueParse"]];
    NSData *imageData = [theImage getData];
    UIImage *profileImage = [UIImage imageWithData:imageData];

    if (profileImage == nil) 
        profilePicture.image = [UIImage imageNamed:@"765-default-avatar.png"];
     else 
        profilePicture.image = profileImage;
    

];

【问题讨论】:

您可能已经有了答案,但我看到一个拼写错误:您使用密钥“userPictureParse”保存并使用“userPictrueParse”检索它。您的日志中有任何错误吗? 【参考方案1】:

您可以使用 ParseImageView 代替 ImageView 并通过以下代码设置图像-

try
    ParseFile image = ParseUser.getCurrentUser().getParseFile("picture");
    if (image != null) 

        parseImgView.setParseFile(image);
        parseImgView.loadInBackground();
    
catch(Exception e)

    e.printStackTrace();

【讨论】:

这比我一直在看的其他东西要简单得多。谢谢。【参考方案2】:

将图像文件上传到 Parse 可能存在问题,因此您还必须保存 parse-image-file “userProfilePictureFileContainer”。因此,通过以下代码更改您的方法-

-(IBAction)saveButtonAction:(id)sender 

    PFUser *currentUserSave = [PFUser currentUser];

    userBioString = userBio.text;
    genderFieldString = genderField.text;
    ageFieldString = ageField.text;

    profilePictureData = UIImageJPEGRepresentation(profilePicture.image, .05f);
    PFFile *userProfilePictureFileContainer = [PFFile fileWithData:profilePictureData];

    [userProfilePictureFileContainer saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
    
        if (!error) 
            //IMAGE SAVED NOW SET TO USER

        currentUserSave[@"userBioParse"] = userBioString;
        currentUserSave[@"userGenderParse"] = genderFieldString;
        currentUserSave[@"ageFieldParse"] = ageFieldString;
        [currentUserSave setObject:userProfilePictureFileContainer forKey:@"userPictureParse"];
        userImageData = UIImageJPEGRepresentation(profilePicture.image, 0.5);
        userProfileImageFile = [PFFile fileWithData:userImageData];
        [currentUserSave setObject:userProfileImageFile forKey:@"userPictureParse"];

        [[PFUser currentUser] saveInBackground];
        
        else
        
        // ERROR OCCURED
        
    ];

【讨论】:

哦,不,不,对不起,误会了。我没有任何问题保存我在从解析中检索文件时遇到问题。【参考方案3】:

对于任何有同样问题的人,我必须做的就是将我的 profilePicture 类从 UIImageView 切换到 PFImageView。完成后,我可以使用此代码从解析中调用文件。

PFUser *currentuser = [PFUser currentUser];
PFFile *pictureFile = [currentuser objectForKey:@"userPictureParse"];
profilePicture.file = pictureFile;
[profilePicture loadInBackground];

超级超级简单。谢谢大家的帮助。

【讨论】:

以上是关于检索附加到 currentUser / Parse 的照片的主要内容,如果未能解决你的问题,请参考以下文章

无法通过查询从 Parse 数据库中检索用户数据

使用 getdatainbackground 从 Parse 检索多个图像(IOS - Swift)

将文档数据检索到一个列表中,其中 documentID 与 currentUser().uid 相同 - Flutter

如何知道解析服务器中的 currentUser

Flutter/dart:Parse Server sdk:ParseUser.currentUser() 函数在重置时返回 null

IOS Parse Homescreen when currentUser() != nil