How can I convert FBProfilePictureView to an UIIma

2019-01-11 11:03发布

问题:

Everything is working fine with FBProfilePictureView but I need to get that picture from FBProfilePictureView and turn it into an UIImage.

How should I do it?

I tried using this:

UIGraphicsBeginImageContext(self.profilePictureView.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.TestPictureOutlet.image = viewImage;

But this doesnt work for my solution.

回答1:

FBProfilePictureView is a UIView, this UIView contains a UIImageView, that is your image, you can get the UIImage from that UIImageView:

profilePictureView is a FBProfilePictureView

UIImage *image = nil;

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

EDIT: add another way more quickly but do the same thing

__block UIImage *image = nil;

[self.view.subviews enumerateObjectsUsingBlock:^(NSObject *obj, NSUInteger idx, BOOL *stop) {
    if ([obj isMemberOfClass:[UIImageView class]]) {
        UIImageView *objImg = (UIImageView *)obj;
        image = objImg.image;
        *stop = YES;
    }
}];


回答2:

Above both mentioned solutions work fine to get UIImage object out from FBProfilePictureView. Only thing is, You need to put some delay before to get image from FBProfilePictureView. Like:

[FBRequest requestForMe] startWithCompletionHandler:
     ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
         if (!error) {

             myNameLbl.text = user.name;
             profileDP.profileID = user.id;

//NOTE THIS LINE WHICH DOES THE MAGIC

[self performSelector:@selector(getUserImageFromFBView) withObject:nil afterDelay:1.0];

}];

- (void)getUserImageFromFBView{

    UIImage *img = nil;

     //1 - Solution to get UIImage obj

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

    //2 - Solution to get UIImage obj

//    UIGraphicsBeginImageContext(profileDP.frame.size);
//    [profileDP.layer renderInContext:UIGraphicsGetCurrentContext()];
//    img = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();

//Here I'm setting image and it works 100% for me.

   testImgv.image = img;

}

Regards!

Aamir Ali - iOS Apps Developer

@Time Group (TGD)



回答3:

Here the solution.

steps:

Make sure that you assigned the FB user id to object of class "FBProfilePictureView" in my case this class object is "userPictureImageView"

-(void)saveFBUserImage
{

    CGSize imageSize = self.userPictureImageView.frame.size;

    UIGraphicsBeginImageContext(imageSize);
    CGContextRef imageContext = UIGraphicsGetCurrentContext();

    [self.userPictureImageView.layer renderInContext: imageContext];
    UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    NSData *imageData = UIImageJPEGRepresentation(viewImage,1);

    UIImage * img = [UIImage imageWithData:imageData]; 

    NSString *filePath  =  <specify your path here>;

    CGSize size = img.size;

    if(size.height > 50)
        size.height = 50;
    if(size.width > 50)
        size.width = 50;

    CGRect rect = CGRectMake(0.0f, 0.0f,  size.width, size.height); 
    CGSize size2 = rect.size;

    UIGraphicsBeginImageContext(size2);

    [img drawInRect:rect];
    img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSData *newImageData = UIImageJPEGRepresentation(img, 1.0);
    [newImageData writeToFile:filePath atomically:YES];
}

That's it. :-)



回答4:

I have found above solutions to be quite working but here is updated code which i found it more easy going.

 @property FBSDKProfilePictureView *pictureView;

 if ([FBSDKAccessToken currentAccessToken]) {


    self.pictureView=[[FBSDKProfilePictureView alloc]init];

    [self.pictureView setProfileID:@"me"];
    [self.pictureView setPreservesSuperviewLayoutMargins:YES];
    [self.pictureView setPictureMode:FBSDKProfilePictureModeNormal];
    [self.pictureView setNeedsImageUpdate];
    [self performSelector:@selector(getUserImageFromFBView) withObject:nil afterDelay:1.0];

}

-(void) getUserImageFromFBView
{
UIImage *image = nil;

for (NSObject *obj in [self.pictureView subviews]) {
    if ([obj isMemberOfClass:[UIImageView class]]) {
        UIImageView *objImg = (UIImageView *)obj;
        image = objImg.image;
        break;
    }
}
[self.profilePic setImage:image forState:UIControlStateNormal];
}

Hope this helps. Here i have put 1 second delay to wait for the profile picture to load.