Search code examples
objective-cxcodemacoscocoauser-accounts

Programmatically changing the User's account image in OSX


Is there some way I can programmatically change the users account image on OSX?

I know I can retrieve it, but can it be changed like apple does on the account page in the settings app?


Solution

  • You can use the Address Book framework. You need to use the me method to get the record for the current user, and then the setImageData: method to set the user's image:

    #import <AddressBook/AddressBook.h>
    
    @implementation YourClass
    - (void)setUserImage:(NSImage*)anImage
    {
        ABPerson* me = [[ABAddressBook addressBook] me];
        [me setImageData:[anImage TIFFRepresentation]];
    }
    @end
    

    There's more detail here in the docs.