简体   繁体   中英

Turn FBProfilePictureView into UIImage

I have a Facebook login that gets the users profilePicture, lblUsername, and lblEmail . I know that the FBProfilePictureView is a UIView that contains a UIImageView.

So, what I am asking is how do I convert or "save" the profilePicture that you get from Facebook as a UIImage? Here is my LoginViewController.m . I have imported the Facebook SDK correctly in the LoginViewController.h file.

#import "LoginViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface LoginViewController ()

- (void)toggleHiddenState:(BOOL)shouldHide;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self toggleHiddenState:YES];
    self.lblLoginStatus.text = @"";

    self.loginButton.readPermissions = @[@"public_profile", @"email"];
    self.loginButton.layer.cornerRadius = 0;
    [self.loginButton.layer setBorderWidth:0.0f];

    self.loginButton.delegate = self;


//    UIImageView *image = nil;
//    
//    for (NSObject *pic in [self.profilePicture subviews]) {
//        if ([pic isMemberOfClass:[UIImageView class]]) {
//            UIImageView *objImg = (UIImageView *)pic;
//            image = objImg.image;
//            break;
//        }
//    }
//    
//    self.view.backgroundColor = [UIColor colorWithPatternImage:image];
//
    // Do any additional setup after loading the view.
}

-(void)toggleHiddenState:(BOOL)shouldHide{
    self.lblUsername.hidden = shouldHide;
    self.lblEmail.hidden = shouldHide;
    self.profilePicture.hidden = shouldHide;
    self.loggedinwallpaper.hidden = shouldHide;
    self.FBlogout.hidden = shouldHide;
}

-(void)toggleUnhiddenState:(BOOL)shouldShow{
    self.loggedinwallpaper.hidden = NO;
}

-(void)loginViewShowingLoggedInUser:(FBLoginView *)loginView{
    self.lblLoginStatus.text = @"";

    [self toggleHiddenState:NO];
    [self toggleUnhiddenState:YES];
}

-(void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView{
    self.lblLoginStatus.text = @"";

    [self toggleHiddenState:YES];
}

-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user{
    NSLog(@"%@", user);
    self.profilePicture.profileID = user.objectID;
    self.lblUsername.text = user.name;
    self.lblEmail.text = [user objectForKey:@"email"];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *homeViewController = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"];
    [self presentViewController:homeViewController animated:YES completion:nil];

//    
//    for (NSObject *obj in [self.profilePicture subviews]) {
//        if ([obj isMemberOfClass:[UIImageView class]]) {
//            UIImageView *objImg = (UIImageView *)obj;
//            image = objImg.image;
//            break;
//        }
//    }
}



-(void)loginView:(FBLoginView *)loginView handleError:(NSError *)error{
    NSLog(@"%@", [error localizedDescription]);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//    UIImage *image = _profilePicture;
//    HomeViewController *homeviewController = (HomeViewController *)segue.destinationViewController;
//    homeviewController.homepic = image;
//

    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
//}


@end

I would not use the image like this since facebook will load different sizes of that image and you don't know whether you will just get a low quality version. Can't you just download the image for yourself in the size you need it?

Facebook uses urls like this: http://graph.facebook.com/USER_ID/picture?width=WIDTH&height=HEIGHT

ofc you need to swap out USER_ID for the user's facebook user id and WIDTH and HEIGHT to values you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM