Get User Account Image

后端 未结 3 1058
故里飘歌
故里飘歌 2020-12-15 14:09

Simply: how do I get the user\'s account image?

I\'m using Cocoa on Mac.

相关标签:
3条回答
  • 2020-12-15 14:25

    The Address Book method doesn't work if the user deletes the vcard.

    I use the CBIdentity method that never breaks, as Dave says:

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

    In order to use CBIdentity you need to add the Collaboration Framework to your targets and use the following include directive

    #import <Collaboration/Collaboration.h>
    
    0 讨论(0)
  • 2020-12-15 14:38

    If you can get a handle to a CSIdentityRef or a CBIdentity* that represents the user in question, then you can invoke the -[CBIdentity image] method to retrieve their account image.

    Edit:

    Here's a previous answer of mine that shows how to query for all standard user accounts on a system and convert them into CBIdentity objects: Get all Users on OS X

    If you don't want to link against Collaboration.framework, then you can use something like CSIdentityImageGetData (or one of the similar variants) to get the image directly. I personally find working with a native Cocoa object to be nicer, but in this case it's not absolutely necessary.

    0 讨论(0)
  • 2020-12-15 14:49

    If you only want an image for the logged-in user, it's also possible to get it with a one-liner using AddressBook:

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

    but I believe that this is not guaranteed to be the same as the log-in image.

    0 讨论(0)
提交回复
热议问题