Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Getting and displaying album artwork from iPod library

I want to get the album art from a audiobook in the iPod library, But I do not understand how to use MPMediaItemPropertyArtwork

- (NSArray *)audiobooks
{
    MPMediaPropertyPredicate *abPredicate =
    [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInt:MPMediaTypeAudioBook]
                                     forProperty:MPMediaItemPropertyMediaType];

    MPMediaQuery *abQuery = [[MPMediaQuery alloc] init];
    [abQuery addFilterPredicate:abPredicate];
    [abQuery setGroupingType:MPMediaGroupingAlbum];
    NSArray *books = [abQuery collections];
    return books;
}

- (MPMediaItem *)mediaItemForRow: (NSInteger)row
{
    NSArray *audiobooks = self.audiobooks;
    MPMediaItem *mediaItem = nil;

    for (id object in audiobooks) {
        if ([object isKindOfClass:[MPMediaItemCollection class]]) {
            MPMediaItemCollection *book = (MPMediaItemCollection *)object;

            id item = [book items][row];
            if ([item isKindOfClass:[MPMediaItem class]]) {
                mediaItem = (MPMediaItem *)item;

            }
        }
    }
    return mediaItem;
}

So now I can get title of media item like this:

NSString *title = [mediaItem valueForProperty:MPMediaItemPropertyArtist];

But how do i get the artwork so I can display it in a UIImage? there is this property:

[mediaItem valueForProperty:MPMediaItemPropertyArtwork]

I have not managed to find out how to use it.

like image 532
Tom Lilletveit Avatar asked Aug 25 '13 08:08

Tom Lilletveit


2 Answers

I have just converted the above answer to Swift 3:

let songInfo: MPMediaItem = self.arrSongs[indexPath.row] as! MPMediaItem

// Song Info is Media Item.

 let itemArtwork :MPMediaItemArtwork  = songInfo.artwork!
 cell.songImg.image = itemArtwork.image(at: CGSize(width: 50, height: 50))
like image 39
Ketan P Avatar answered Sep 24 '22 19:09

Ketan P


You should be able to fetch the artwork into a UIImage via the following:

MPMediaItemArtwork *itemArtwork = [mediaItem valueForProperty:MPMediaItemPropertyArtwork];
UIImage *artworkUIImage = [itemArtwork imageWithSize:CGSizeMake(64, 64)];

In essence, the MPMediaItemPropertyArtwork property returns a MPMediaItemArtwork which you can then obtain a UIImage from.

like image 155
John Parker Avatar answered Sep 23 '22 19:09

John Parker