iOS Google 无法登录获取用户个人资料图片?(不是 Google 加上登录)
iOS Google Sign-in fetching user profile picture impossible?(NOT Google plus sign-in)
我正在尝试让 google 在我的应用程序中登录,但遇到了问题。
(不是 google plus 登录,我使用的是 google 登录)
我关注了 this link 并且有效。我得到用户 ID、user.authentication.idToken、user.profile.name 和 user.profile.email。
但是我找不到获取用户头像的方法。
以上 google 文档对此没有评论。
我在网上搜索了几个小时,但发现了大约 google 加上我不想要的登录。
我在我的应用程序中实现了 Facebook 登录,他们提供了 url 来获取用户个人资料图片。
google 登录是否提供类似的服务?
这是我获取用户信息但没有图片的工作代码。
请帮忙
- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error {
if (user) {
NSString *userId = user.userID; // For client-side use only!
NSString *token = user.authentication.idToken; // Safe to send to the server
NSString *name = user.profile.name;
NSString *email = user.profile.email;
...
}
你必须设置 [GIDSignIn sharedInstance].shouldFetchBasicProfile = YES;并使用下面的代码获取个人资料图片
if ([GIDSignIn sharedInstance].currentUser.profile.hasImage)
{
NSUInteger dimension = round(thumbSize.width * [[UIScreen mainScreen] scale]);
NSURL *imageURL = [user.profile imageURLWithDimension:dimension];
}
Swift 实施:
let dimension = round(thumbSize.width * UIScreen.mainScreen().scale);
let pic = user.profile.imageURLWithDimension(dimension)
Swift 3
let dimension = round(imageSize.width * UIScreen.main.scale)
let pic = userInfo.profile.imageURL(withDimension: dimension)
imageSize.width 是要求的图像宽度。
let dimension = round(100 * UIScreen.main.scale)
let pic = userInfo.profile.imageURL(withDimension: dimension)
谢谢阿米特
BOOL hashImage=user.profile.hasImage;
CGSize thumbSize=CGSizeMake(500, 500);
if (hashImage) {
NSUInteger dimension = round(thumbSize.width * [[UIScreen mainScreen] scale]);
NSURL *imageURL = [user.profile imageURLWithDimension:dimension];
NSLog(@"image url=%@",imageURL);
}
Swift 5
您需要通过此电话登录您的 google 帐户:
GIDSignIn.sharedInstance().signIn()
然后当电话在这里接到电话时:
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if let error = error {
// Mange error
} else {
let dimension = round(MY_UIIMAGE_VIEW.bounds.width * UIScreen.main.scale)
let pic = user.profile.imageURL(withDimension: UInt(dimension))
print("Image URL: \(pic)")
}
}
这是为我工作的代码。这是@Techbee 对 Swift 5 的编辑。我正在使用图像视图显示图像,它有一个 75x75 帧,从 API 返回的图像是 225x225 jpg。
我正在尝试让 google 在我的应用程序中登录,但遇到了问题。
(不是 google plus 登录,我使用的是 google 登录)
我关注了 this link 并且有效。我得到用户 ID、user.authentication.idToken、user.profile.name 和 user.profile.email。
但是我找不到获取用户头像的方法。 以上 google 文档对此没有评论。
我在网上搜索了几个小时,但发现了大约 google 加上我不想要的登录。
我在我的应用程序中实现了 Facebook 登录,他们提供了 url 来获取用户个人资料图片。
google 登录是否提供类似的服务?
这是我获取用户信息但没有图片的工作代码。
请帮忙
- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error {
if (user) {
NSString *userId = user.userID; // For client-side use only!
NSString *token = user.authentication.idToken; // Safe to send to the server
NSString *name = user.profile.name;
NSString *email = user.profile.email;
...
}
你必须设置 [GIDSignIn sharedInstance].shouldFetchBasicProfile = YES;并使用下面的代码获取个人资料图片
if ([GIDSignIn sharedInstance].currentUser.profile.hasImage)
{
NSUInteger dimension = round(thumbSize.width * [[UIScreen mainScreen] scale]);
NSURL *imageURL = [user.profile imageURLWithDimension:dimension];
}
Swift 实施:
let dimension = round(thumbSize.width * UIScreen.mainScreen().scale);
let pic = user.profile.imageURLWithDimension(dimension)
Swift 3
let dimension = round(imageSize.width * UIScreen.main.scale)
let pic = userInfo.profile.imageURL(withDimension: dimension)
imageSize.width 是要求的图像宽度。
let dimension = round(100 * UIScreen.main.scale)
let pic = userInfo.profile.imageURL(withDimension: dimension)
谢谢阿米特
BOOL hashImage=user.profile.hasImage;
CGSize thumbSize=CGSizeMake(500, 500);
if (hashImage) {
NSUInteger dimension = round(thumbSize.width * [[UIScreen mainScreen] scale]);
NSURL *imageURL = [user.profile imageURLWithDimension:dimension];
NSLog(@"image url=%@",imageURL);
}
Swift 5
您需要通过此电话登录您的 google 帐户:
GIDSignIn.sharedInstance().signIn()
然后当电话在这里接到电话时:
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if let error = error {
// Mange error
} else {
let dimension = round(MY_UIIMAGE_VIEW.bounds.width * UIScreen.main.scale)
let pic = user.profile.imageURL(withDimension: UInt(dimension))
print("Image URL: \(pic)")
}
}
这是为我工作的代码。这是@Techbee 对 Swift 5 的编辑。我正在使用图像视图显示图像,它有一个 75x75 帧,从 API 返回的图像是 225x225 jpg。