Simply: how do I get the user's account image?
I'm using Cocoa on Mac.
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>
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With