所有tableView单元格显示播放列表的相同图稿图像 [英] All tableView Cells Show Same Artwork Image for Playlists

查看:78
本文介绍了所有tableView单元格显示播放列表的相同图稿图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tableView,其中显示了用户音乐库中的播放列表.我用来获取此列表的代码是:

I have a tableView which shows a list of playlists from the user's music library. The code I used to get this list is:

@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?

However, this only returns the same artwork image for all cells in my table. How do I run getAlbumArtworkWithSize for each table view cell (playlist name)?

这是我目前得到的:

推荐答案

这是因为您没有遍历getAlbumArtworkWithSize方法中属于该行的播放列表所特有的项目集合.为了简化操作并减少执行时间(不对方法进行额外查询),请尝试将集合传递给函数:

It's because you're not iterating through the collection of items specific to the playlist belonging to that row in your getAlbumArtworkWithSize method. To make it easier and reduce execution time (not making an additional query in your method), try passing the collection to your function:

- (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个项目的播放列表可能会非常慢.最好使用集合中的代表项,如果没有图像返回,则使用占位符.想要这样的东西:

I wouldn't recommend using this method, it could prove to be very slow with playlists containing ~1000 items. It's better to use the representative item of the collection and use a placeholder if no image is returned. Which would like something like this:

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

这篇关于所有tableView单元格显示播放列表的相同图稿图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆