16

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

我该怎么做?

我尝试使用这个:

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

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

4

4 回答 4

17

FBProfilePictureView 是一个 UIView,这个 UIView 包含一个 UIImageView,也就是你的图片,你可以从那个 UIImageView 中获取 UIImage:

profilePictureView 是一个 FBProfilePictureView

UIImage *image = nil;

for (NSObject *obj in [profilePictureView subviews]) {
    if ([obj isMemberOfClass:[UIImageView class]]) {
        UIImageView *objImg = (UIImageView *)obj;
        image = objImg.image;
        break;
    }
}

编辑:更快地添加另一种方式,但做同样的事情

__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;
    }
}];
于 2012-09-18T14:55:45.607 回答
3

上面提到的两个解决方案都可以很好地从 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 应用程序开发人员

@时代集团 (TGD)

于 2013-01-31T11:55:58.743 回答
0

这里的解决方案。

脚步:

确保您将 FB 用户 ID 分配给“FBProfilePictureView”类的对象,在我的情况下,此类对象是“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];
}

而已。:-)

于 2013-01-29T12:41:55.163 回答
0

我发现上述解决方案非常有效,但这里是更新的代码,我发现它更容易上手。

 @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 秒以等待个人资料图片加载。

于 2015-05-14T08:31:56.537 回答