0

我正在使用 Parse 和 Facebook 登录来创建一个新用户。我从 Facebook 获得了一些字段,例如他们的姓名和位置,请求他们的个人资料图像在后台加载,然后我转到具有个人资料图像的 PFImageView 的主视图控制器。

问题是 PFFile 用户 [@"profileImage"] 到主视图控制器的 viewDidLoad 触发时仍然为零,因此没有什么可以加载 InBackground。

我不想在进入主视图控制器之前等待配置文件图像加载......这将花费一些随机时间并且很糟糕。

那么,如何处理在将其提供给 PFFImageView 之前尚未加载的 PFFile?某种占位符?

这是我的代码:

------- in LoginViewController.m

- (void) pressedFacebookLogin {
    [PFFacebookUtils logInWithPermissions...
        if(user.isNew) {
           FBRequest *request = [FBRequest requestForMe];
           [request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                NSDictionary *userData = (NSDictionary *)result;    

                user[@"fullName"] = userData[@"name"];
                // get location, relationship if available
                [user saveInBackground];

                // start getting the user's profile image before going to main view controller
                NSURL *pictureURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large&return_ssl_resources=1", facebookID]];
                NSURLRequest *urlRequest = [NSURLRequest requestWithURL:pictureURL];
                [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

                    if (connectionError == nil && data != nil) {
                        UIImage *tmpImage = [UIImage imageWithData:data];
                        NSData *imageData = UIImagePNGRepresentation(tmpImage);
                        PFFile *imageFile = [PFFile fileWithName:@"image.png" data:imageData];
                        user[@"profileImage"] = imageFile;
                        [user saveInBackground];
                    }
                }];

                [self segueToMainViewController];

           }];
        }else {
             // deal with existing user...
        }
    }];
}

-------------- in MainViewController.m

- (void) viewDidLoad {
    [super viewDidLoad];

    PFUser *user = [PFUser currentUser];

    self.profileImageView.image = [UIImage imageNamed:@"placeholder"];
    PFFile *profileImageFile = user[@"profileImage"];
    // at this point profileImageFile is nil
    self.profileImageView.file = profileImageFile;
    [self.profileImageView loadInBackground];

}
4

1 回答 1

0

在您将 profileImageFile 设为空的地方添加此行。

我猜你使用的 profileImageView 是一个 ImageView。

@property (weak, nonatomic) IBOutlet UIImageView *profileImageView;

// at this line where you get profileImageFile as nil
[self.profileImageView setImageWithURL:[NSURL URLWithString:user[@"profileImage"]] placeholderImage:[UIImage imageNamed:@"default_profileImage"]];
于 2014-10-24T05:13:45.647 回答