How can I convert FBProfilePictureView to an UIImage?

前端 未结 4 761
遥遥无期
遥遥无期 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: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  =  ;
    
        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. :-)

提交回复
热议问题