6

MPNowPlayingInfoCenter の MPMediaItemArtwork を設定するために私が見つけたすべてのメソッドは、ローカル画像を使用しています。MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage: [UIImage imageNamed:@"myimage"];

しかし、これをimageURLから設定する必要があります

現在、私はこれを使用しています...

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

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

何か案が?

4

2 に答える 2

7

それが私の最善の解決策です:

- (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; 
    });
}

/!\ を既に設定している場合はMPNowPlayingInfoCenter、それを取得しないと、他のすべての値が上書きされます

于 2015-03-16T10:03:34.670 に答える