如何在UIImageView中从“User”类中获取用户图像? Parse.com

时间:2014-10-05 14:07:03

标签: ios objective-c parsing uiimageview parse-platform

我想从Parse的UIImageView用户下载图像。在User部分中,我创建了UserImage部分,但我无法将其下载到UIImageView中,我该怎么办?你帮个忙吗?请帮帮我。

我已使用此代码:

PFUser* currentUser = [PFUser currentUser];
if (currentUser) {

    PFQuery* queryPhoto = [PFQuery queryWithClassName:@"UserImage"];

    [queryPhoto findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

        // Verify if there are no errors
        if (!error) {

            // Retrieve Usename
            User.text = [NSString stringWithFormat:@"%@",[[PFUser currentUser]valueForKey:@"username"]] ;

            // Retrieve Photo

            PFUser *currentuser = [PFUser currentUser];
            PFFile *file = [currentuser objectForKey:@"UserImage"];

            [file getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {

                if (!error) {
                    UIImage *image = [UIImage imageWithData:data];
                    // image can now be set on a UIImageView

                    self.UserImages.image = image;
                }
            }];
        }

        else {
            NSString *errorString = [[error userInfo] objectForKey:@"error"];
            UIAlertView *errorAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [errorAlertView show];

        }

    } ];

}

1 个答案:

答案 0 :(得分:1)

Parse.com专门用PFImageView来完成这项任务。

试试这个:

PFUser* currentUser = [PFUser currentUser];
if (currentUser) {
    PFQuery* queryPhoto = [PFQuery queryWithClassName:@"UserImage"];

    [queryPhoto findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

        // Verify if there are no errors
        if (!error) {

            // Retrieve Usename
            User.text = [NSString stringWithFormat:@"%@",[[PFUser currentUser]valueForKey:@"username"]] ;

            // Retrieve Photo & Load
            PFFile *file = [currentUser objectForKey:@"UserImage"];
            PFImageView *imageView = [[PFImageView alloc] init]; 
            self.UserImages.image = imageView; //Assuming here that you set self.UserImages.image as a PFImageView
            self.UserImages.image.file = file; 
            [self.UserImages.image loadInBackground]; 
       }

        else {
            NSString *errorString = [[error userInfo] objectForKey:@"error"];
            UIAlertView *errorAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [errorAlertView show];

        }

    }];
}