Search code examples
iosfacebook-graph-apiuiimagefacebook-ios-sdk

How can I convert FBProfilePictureView to an UIImage?


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.


Solution

  • 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;
        }
    }];