How can I convert FBProfilePictureView to an UIImage?

前端 未结 4 745
遥遥无期
遥遥无期 2020-12-03 05:18

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?

相关标签:
4条回答
  • 2020-12-03 06:05

    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)

    0 讨论(0)
  • 2020-12-03 06:08

    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.

    0 讨论(0)
  • 2020-12-03 06:09

    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;
        }
    }];
    
    0 讨论(0)
  • 2020-12-03 06:13

    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. :-)

    0 讨论(0)
提交回复
热议问题