如何将FBProfilePictureView转换为UIImage?
一切正常FBProfilePictureView工作正常,但我需要从FBProfilePictureView得到的图片,并把它变成一个UIImage。
我应该怎么做?
我试着用这个:
UIGraphicsBeginImageContext(self.profilePictureView.frame.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); self.TestPictureOutlet.image = viewImage;
但是这不适用于我的解决scheme。
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; } }];
上面提到的两个解决scheme都可以很好地从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)
这里的解决scheme。
脚步:
请确保您将FB用户标识分配给类“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]; }
而已。 🙂
我发现上面的解决scheme是相当工作,但这里是更新的代码,我发现它更容易。
@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秒的延迟,等待configuration文件图片加载。
- 通过Facebook Graph API获取个人资料图片返回“不支持的URL”
- 在Facebook用户的Xcode中查看ProfilePicture
- 如何发布Facebook上的路线图不是图像
- 如何实现FacebookgraphicsAPI的注销function
- FBSessionDelegate方法不会触发
- 如何移动到使用iOS SDK的Facebook JSON响应中的下一页?
- Facebook API:如何获取FBlogin用户的唯一ID? 来自API的ID与v2.0中的“真实”用户ID不匹配
- 在iOS应用程序中pipe理Facebook好友的最佳实践
- Facebook API – 如何取消graphics请求