首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将FBProfilePictureView转换为UIImage?

如何将FBProfilePictureView转换为UIImage?
EN

Stack Overflow用户
提问于 2012-08-10 02:29:21
回答 2查看 7.9K关注 0票数 16

使用FBProfilePictureView一切正常,但我需要从FBProfilePictureView获取图片并将其转换为UIImage。

我怎么发动汽车呢?

我试着使用这个:

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

但这对我的解决方案不起作用。

EN

回答 2

Stack Overflow用户

发布于 2013-01-31 19:55:59

上面提到的两种解决方案都可以很好地从FBProfilePictureView中获取UIImage对象。唯一的问题是,您需要在从FBProfilePictureView获取图像之前进行一些延迟。像这样:

[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;

}

致以问候!

Aamir Ali - iOS应用程序开发人员

@Time Group (TGD)

票数 3
EN

Stack Overflow用户

发布于 2015-05-14 16:31:57

我发现上面的解决方案是相当有效的,但这里是更新的代码,我发现它更容易。

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

希望这能有所帮助。在这里,我设置了1秒的延迟,以等待资料图片加载。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11889725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档