获取用户帐户图像

时间:2011-03-24 18:31:30

标签: cocoa macos

简单地说:如何获取用户的帐户图片?

我在Mac上使用Cocoa。

3 个答案:

答案 0 :(得分:7)

地址簿方法如果用户删除vcard ,则无效。

我使用永远不会破坏的 CBIdentity 方法,正如戴夫所说:

+ (NSImage *)userImage
{
    CBIdentity *identity = [CBIdentity identityWithName:NSUserName() authority:[CBIdentityAuthority defaultIdentityAuthority]];
    return [identity image];
}

要使用CBIdentity,您需要将协作框架添加到目标并使用以下include指令

#import <Collaboration/Collaboration.h>

答案 1 :(得分:4)

如果您只想为登录用户创建图像,也可以使用AddressBook通过单行获取图像:

NSData *imgData = [[[ABAddressBook sharedAddressBook] me] imageData];

但我认为这不是保证与登录图像相同。

答案 2 :(得分:3)

如果您可以获得代表相关用户的CSIdentityRefCBIdentity*的句柄,那么您可以调用-[CBIdentity image]方法来检索其帐户图片。

编辑:

以上是我之前的答案,其中显示了如何查询系统上的所有标准用户帐户并将其转换为CBIdentity个对象:Get all Users on OS X

如果您不想链接Collaboration.framework,那么您可以使用CSIdentityImageGetData(或其中一个类似的变体)来直接获取图像。我个人觉得使用本机Cocoa对象更好,但在这种情况下,它并非绝对必要。

相关问题