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;
}
I then have 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"];
}
What this code does: it cycles through all songs in the playlist until it finds a song with album artwork, as some songs do not have any artwork. Then it returns that image.
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)?
This is what I currently get:
.