Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get User Account Image

Tags:

macos

cocoa

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

I'm using Cocoa on Mac.

like image 645
Seb Jachec Avatar asked Mar 24 '11 18:03

Seb Jachec


3 Answers

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>
like image 120
kilianc Avatar answered Sep 24 '22 01:09

kilianc


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.

like image 20
jscs Avatar answered Sep 26 '22 01:09

jscs


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.

like image 33
Dave DeLong Avatar answered Sep 25 '22 01:09

Dave DeLong