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?
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
let mpic = MPNowPlayingInfoCenter.default()
DispatchQueue.global().async {
if let urlString = yourUrlString, let url = URL(string:urlString) {
if let data = try? Data.init(contentsOf: url), let image = UIImage(data: data) {
let artwork = MPMediaItemArtwork(boundsSize: image.size, requestHandler: { (_ size : CGSize) -> UIImage in
return image
})
DispatchQueue.main.async {
mpic.nowPlayingInfo = [
MPMediaItemPropertyTitle:"Title",
MPMediaItemPropertyArtist:"Artist",
MPMediaItemPropertyArtwork:artwork
]
}
}
}
}
That worked for me in iOS 11, swift 4
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