Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 966
Anes Hasic Avatar asked Aug 09 '12 18:08

Anes Hasic


2 Answers

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;
    }
}];
like image 137
busta117 Avatar answered Oct 01 '22 18:10

busta117


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)

like image 30
user2028902 Avatar answered Oct 01 '22 18:10

user2028902