objective-cavplayermpmediaitemartworkmpnowplayinginfocenter

MPNowPlayingInfoCenter : What is the best way to set MPMediaItemArtwork from an Url?


All methods I found to set MPMediaItemArtwork of MPNowPlayingInfoCenter are with local images. MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage: [UIImage imageNamed:@"myimage"];

But I need to set this from an imageURL

Currently i use this...

    UIImage *artworkImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.currentTrack.imageUrl]]];

    MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage: artworkImage];
    [self.payingInfoCenter setValue:albumArt forKey:MPMediaItemPropertyArtwork];

Any idea?


Solution

  • That's my best solution:

    - (void)updateControlCenterImage:(NSURL *)imageUrl
    {       
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_async(queue, ^{
    
                NSMutableDictionary *songInfo = [NSMutableDictionary dictionary]; 
                UIImage *artworkImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
                if(artworkImage)
                {
                    MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage: artworkImage];
                    [songInfo setValue:albumArt forKey:MPMediaItemPropertyArtwork];
                }
               MPNowPlayingInfoCenter *infoCenter = [MPNowPlayingInfoCenter defaultCenter];
              infoCenter.nowPlayingInfo = songInfo; 
        });
    }
    

    /!\ if you've already setted the MPNowPlayingInfoCenter , get it or all other values will be overridden