我想从iPod库中的有声读物中获得专辑艺术,但我不知道如何使用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;
}
所以现在我可以得到这样的媒体项目的标题:
NSString *title = [mediaItem valueForProperty:MPMediaItemPropertyArtist];
但是我如何获得艺术品,以便在UIImage中显示它呢?有这样的属性:
[mediaItem valueForProperty:MPMediaItemPropertyArtwork]
我还没找到如何使用它。
您应该能够通过以下方式将艺术品提取到UIImage中:
MPMediaItemArtwork *itemArtwork = [mediaItem valueForProperty:MPMediaItemPropertyArtwork];
UIImage *artworkUIImage = [itemArtwork imageWithSize:CGSizeMake(64, 64)];
本质上,MPMediaItemPropertyArtwork
属性返回一个MPMediaItemArtwork,然后您可以从中获取UIImage。
我刚刚将上面的答案转换为Swift 3:
let songInfo: MPMediaItem = self.arrSongs[indexPath.row] as! MPMediaItem
//歌曲信息是媒体项目。
let itemArtwork :MPMediaItemArtwork = songInfo.artwork!
cell.songImg.image = itemArtwork.image(at: CGSize(width: 50, height: 50))