我正在使用Parse和Facebook登录来创建新用户。我从Facebook获得了一些字段,比如他们的姓名和位置,请求他们在背景中加载他们的个人资料图片,然后再切换到我的主视图控制器,该控制器有一个PFImageView用于个人资料图像。
问题是,当主视图控制器的viewDidLoad触发时,PFFile用户[@" profileImage"]仍然是零,因此没有任何东西可以加载到背景中。
我不想在转到主视图控制器之前等待配置文件图像加载...这需要一些随机时间而且只是坏事。
那么,在我将它提供给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];
}
答案 0 :(得分: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"]];