UIImage don't show as MPMediaItemPropertyArtwork

This is on Xcode Version 7.3.1 (7D1014) on OS X 10.11.5 (15F34), target is an iOS application, built for iOS 9.

I have an audio application in which I cannot manage to show the cover art in Now Playing lock screen.

I can show a static image and other information but I'm not able to show the "real" album cover. Code below works correctly (a 512x512 image is displayed:

        NSMutableDictionary __block *mpInfo = [[NSMutableDictionary alloc] initWithDictionary:[[MPNowPlayingInfoCenter defaultCenter] nowPlayingInfo]];
        mpInfo[MPMediaItemPropertyArtist] = currentSong.artist;
        mpInfo[MPMediaItemPropertyTitle] = currentSong.title;
        if([[RP4RadioManager sharedManager] isPlaying]) {
            mpInfo[MPNowPlayingInfoPropertyPlaybackRate] = @(1.0);
        } else {
            mpInfo[MPNowPlayingInfoPropertyPlaybackRate] = @(0.0);
        }
        mpInfo[MPMediaItemPropertyPlaybackDuration] = @([currentSong.duration doubleValue] / 1000.0);
        mpInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = @(0);
        mpInfo[MPMediaItemPropertyArtwork] = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"BigLogo"]];
        [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:mpInfo];


some rows below I recover the cover art from the network and I try to show it:


                coverImage = [[UIImage alloc] initWithCIImage:currentSong.coverImage];
                MPMediaItemArtwork *imageArtwork = [[MPMediaItemArtwork alloc] initWithImage:coverImage];
                mpInfo[MPMediaItemPropertyArtwork] = imageArtwork;
                DLog(@"Setting cover info. Image: %@, imageArtwork: %@ (%@). mpInfo: %@", coverImage, imageArtwork, NSStringFromCGRect(imageArtwork.bounds), mpInfo);
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:mpInfo];
                });


The image is valid (usually 500x500px, but this don't seems significant). The image is originally a .jpg, saved in a CIImage (for OS X comaptibility in common code) and then loaded in a UIImage (the same image shows correctly in a UIImageView).


Changing the line

mpInfo[MPMediaItemPropertyArtwork] = imageArtwork;

with

mpInfo[MPMediaItemPropertyArtwork] = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"no_cover"]];


shows a different static image, so the problem is in the original image but I really cannot understand what the problem is... the log line shows an image and a valid MPMeidaItemArtwork

Setting cover info. Image: <UIImage: 0x14eaeed0>, {500, 500}, imageArtwork: <MPMediaItemArtwork: 0x14eaef00> ({{0, 0}, {500, 500}}). mpInfo: {
    MPNowPlayingInfoPropertyElapsedPlaybackTime = 0;
    MPNowPlayingInfoPropertyPlaybackRate = 1;
    albumTitle = "Radio Paradise";
    artist = "Other Lives";
    artwork = "<MPMediaItemArtwork: 0x14eaef00>";
    playbackDuration = "242.8";
    title = "Tamer Animals";
}

I suspect it's something really stupid but I don't really know how to debug that.

Any help/hint will be appreciated.

Accepted Reply

For future readers with the same problem: I solved the problem.


The point was the the cover image created from the CIImage was somewhat unsuitable. Good to be shown in a UIImageView but not enough good to be used to init a MPMediaArtwork or to be used in a UIImagePNGRepresentation (that returned nil).


I solved by redrawing the image in a Graphic Context and using the resulting image. Long story short, this code works:


                coverImage = [UIImage imageWithCIImage:currentSong.coverImage];
                UIGraphicsBeginImageContext(coverImage.size);
                [coverImage drawInRect:CGRectMake(0, 0, coverImage.size.width, coverImage.size.height)];
                UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
                MPMediaItemArtwork *imageArtwork = [[MPMediaItemArtwork alloc] initWithImage:newImage];
                mpInfo[MPMediaItemPropertyArtwork] = imageArtwork;

Probably the CIImage was in some "unsupported bitmap format" as the documentation of UIImagePNGRepresentation() says... 😉

  • 5 years later this solution still helped me. (Works exactly same in swift)

Add a Comment

Replies

For future readers with the same problem: I solved the problem.


The point was the the cover image created from the CIImage was somewhat unsuitable. Good to be shown in a UIImageView but not enough good to be used to init a MPMediaArtwork or to be used in a UIImagePNGRepresentation (that returned nil).


I solved by redrawing the image in a Graphic Context and using the resulting image. Long story short, this code works:


                coverImage = [UIImage imageWithCIImage:currentSong.coverImage];
                UIGraphicsBeginImageContext(coverImage.size);
                [coverImage drawInRect:CGRectMake(0, 0, coverImage.size.width, coverImage.size.height)];
                UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
                MPMediaItemArtwork *imageArtwork = [[MPMediaItemArtwork alloc] initWithImage:newImage];
                mpInfo[MPMediaItemPropertyArtwork] = imageArtwork;

Probably the CIImage was in some "unsupported bitmap format" as the documentation of UIImagePNGRepresentation() says... 😉

  • 5 years later this solution still helped me. (Works exactly same in swift)

Add a Comment

Hi,


I tried your solution as my artwork is not showing in my car'bluetooth (or very randomly). Any ideas? The iPhone's Lock Screen always show the artwork image but not the bluetooth's screen in my car. I use AVPlayer to play some stream radios and get the image to display I got via iTunes API.


Thanks,