所有tableView单元格显示播放列表的相同图稿图像

时间:2014-05-19 15:04:43

标签: ios cocoa-touch uitableview mpmusicplayercontroller mpmediaitem

我有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 (@"%@", [playlist valueForProperty: MPMediaPlaylistPropertyName]);

        NSArray *songs = [playlist items];

        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"];
}

这段代码的作用是:它循环播放列表中的所有歌曲,直到找到带有专辑封面的歌曲,因为有些歌曲没有任何艺术作品。然后它返回该图像。

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

这是我目前得到的:

This is what I currently get IMAGE http://i58.tinypic.com/29lxmjs.jpg

1 个答案:

答案 0 :(得分:0)

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

- (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"];
}