iTunesみたいに再生中の曲のアートワーク(ジャケット写真)に合わせて背景色と文字色を変える


[yuch_i](http://qiita.com/yuch_i "yuch_i")さんの[iPhoneで再生中の曲情報を取得する方法](http://qiita.com/yuch_i/items/3d7b0186e47cb99b91df "iPhoneで再生中の曲情報を取得する方法")という記事を読んで、「そういえばちょっと前にiTunesみたいに背景色変えるライブラリ使ったなー」と思ったので、書いておきます。

![](http://farm3.staticflickr.com/2813/12742497325_465ac2ebc4_z.jpg)

とは言っても便利なライブラリがあるので、それを使うだけなのですが。。

**LEColorPicker**
GitHub:https://github.com/luisespinoza/LEColorPicker

```pod
pod "LEColorPicker"
```

使い方はめちゃ簡単です。

```objectivec:LEColorPickerの使い方
LEColorPicker* colorpicker = [LEColorPicker new];
LEColorScheme* scheme = [colorpicker colorSchemeFromImage:[UIImage imageNamed:@"sample.jpg"]];
UIColor* backgroundColor = scheme.backgroundColor;
UIColor* primaryTextColor = scheme.primaryTextColor;
UIColor* secondaryTextColor = scheme.secondaryTextColor;
```


```objectivec:コード全文
//--------------------------------------------------
#pragma mark - LifeCycle
//--------------------------------------------------
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    _artworkImageView.userInteractionEnabled = YES;
    
    self.player = [MPMusicPlayerController iPodMusicPlayer];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onNowPlayingItemChanged:)
                                                 name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
                                               object:self.player];
    
    [self.player beginGeneratingPlaybackNotifications];
}

- (void)viewDidUnload
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self.player endGeneratingPlaybackNotifications];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    
}

//--------------------------------------------------
#pragma mark - Notification
//--------------------------------------------------
-(void) onNowPlayingItemChanged:(NSNotification*)ntf {
    MPMediaItem *mediaItem = [self.player nowPlayingItem];
    NSInteger mediaType = [[mediaItem valueForProperty:MPMediaItemPropertyMediaType] integerValue];
    if (mediaType == MPMediaTypeMusic) {
        
        // アートワーク（ジャケット写真）
        MPMediaItemArtwork *artwork = [mediaItem valueForProperty:MPMediaItemPropertyArtwork];
        UIImage* artworkImage = [artwork imageWithSize:CGSizeMake(600.0, 600.0)];
        _artworkImageView.image = artworkImage;
        
        LEColorPicker* colorpicker = [LEColorPicker new];
        LEColorScheme* scheme = [colorpicker colorSchemeFromImage:artworkImage];
        
        //背景色
        _backgroundView.backgroundColor = scheme.backgroundColor;
        
        //曲名
        NSString* title = [mediaItem valueForProperty:MPMediaItemPropertyTitle];
        [_titleLabel setText:title];
        [_titleLabel setTextColor:scheme.primaryTextColor];
        
        //アルバム名
        NSString* albumTitle = [mediaItem valueForProperty:MPMediaItemPropertyAlbumTitle];
        [_albumLabel setText:albumTitle];
        [_albumLabel setTextColor:scheme.primaryTextColor];
        
        //リリース日
        NSDate* releaseDate = [mediaItem valueForProperty:MPMediaItemPropertyReleaseDate];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy/MM/dd"];
        NSString *dateStr = [formatter stringFromDate:releaseDate];
        
        [_releaseDateLabel setText:dateStr];
        [_releaseDateLabel setTextColor:scheme.secondaryTextColor];
        
        // アーティスト名
        NSString* artistName = [mediaItem valueForProperty:MPMediaItemPropertyArtist];
        [_artistLabel setText:artistName];
        [_artistLabel setTextColor:scheme.secondaryTextColor];
    }
}

//--------------------------------------------------
#pragma mark - Action
//--------------------------------------------------
#pragma mark === Tap Artwork ===
//--------------------------------------------------
- (IBAction)actionTapImageView:(id)sender
{
    if( [self.player playbackState] == MPMusicPlaybackStatePlaying ){
        [self.player pause];
    }
    else{
        [self.player play];
    }
}

//--------------------------------------------------
#pragma mark === Swipe Right Artwork ===
//--------------------------------------------------
- (IBAction)actionSwipeRightImageView:(id)sender
{
    [self.player skipToPreviousItem];
}

//--------------------------------------------------
#pragma mark === Swipe Left Artwork ===
//--------------------------------------------------
- (IBAction)actionSwipeLeftImageView:(id)sender
{
    [self.player skipToNextItem];
}
```