Instance Method

loadPhotoForSize:withCompletionHandler:

Loads a photo of this player from Game Center.

Declaration

iOS, tvOS
- (void)loadPhotoForSize:(GKPhotoSize)size withCompletionHandler:(void (^)(UIImage *photo, NSError *error))completionHandler;
macOS
- (void)loadPhotoForSize:(GKPhotoSize)size withCompletionHandler:(void (^)(NSImage *photo, NSError *error))completionHandler;

Parameters

size

A constant that determines the size of the photo to load.

completionHandler

A block to be called when the player data is retrieved from Game Center.

The block receives the following parameters:

photo

An image for the player. If an error occurred, this may still be non-nil. In this case, the image reflects an image cached by Game Kit on the device.

error

If an error occurred, this error object describes the error. If the operation completed successfully, this is nil.

Discussion

Listing 1 shows a typical implementation for loading a player’s photo. There is no property that stores the returned photo. You must write your own code to associate player photos with GKPlayer objects.

Listing 1

Loading a player’s photo

- (void) loadPlayerPhoto: (GKPlayer*) player
{
    [player loadPhotoForSize:GKPhotoSizeSmall withCompletionHandler:^(UIImage *photo, NSError *error) {
        if (photo != nil)
        {
            [self storePhoto: photo forPlayer: player];
        }
        if (error != nil)
        {
            // Handle the error if necessary.
        }
     }];
}

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread.

See Also

Player Photos

GKPhotoSize

The size of a photo loaded by Game Center.