MPMediaItemArtwork returning wrong sized artwork

别来无恙 提交于 2019-12-04 05:27:30

I downloaded the AddMusic sample source from Apple that also uses MPMediaItemArtwork just to see how they handled things.

In that project's MainViewController.m file, these lines:

// Get the artwork from the current media item, if it has artwork.
MPMediaItemArtwork *artwork = [currentItem valueForProperty: MPMediaItemPropertyArtwork];

// Obtain a UIImage object from the MPMediaItemArtwork object
if (artwork) {
    artworkImage = [artwork imageWithSize: CGSizeMake (30, 30)];
}

always returns an image of size 55 x 55 at a scale of 1.0.

I would say MPMediaItemArtwork not respecting the requested size parameters is a bug that you should file via bugreporter.apple.com, although Apple might also have an excuse that "55 x 55" is some optimal size to be displayed on iPads & iPhones.

For blunt force UIImage resizing, I'd recommend using Trevor Harman's "UIImage+Resize" methods found here: http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way

And once you add his category extensions to your project, you could do your desired memory-conserving resizing with a simple call like this:

UIImage *albumCover = [artwork imageWithSize:CGSizeMake(128.0f, 128.0f)];
UIImage *resizedCover = [albumCover resizedImage: CGSizeMake(128.0f, 128.0f) interpolationQuality: kCGInterpolationLow]; 

Using the Trevor Harman's "UIImage+Resize" category it's simple to add resize category to the MPMediaItemArtwork to get the resized image for a certain size and interpolation quality:

@interface MPMediaItemArtwork ()
- (UIImage *)resizedImage:(CGSize)newSize
     interpolationQuality:(CGInterpolationQuality)quality;
@end

@implementation MPMediaItemArtwork (Resize)
- (UIImage *)resizedImage:(CGSize)newSize interpolationQuality:(CGInterpolationQuality)quality {
    return [[self imageWithSize:newSize] resizedImage: newSize interpolationQuality: quality];
}
@end

In this way just call

CGSize thumbnailSize = CGSizeMake(128.0, 128.0);
MPMediaItemArtwork *artwork = [myMediaItem valueForProperty:MPMediaItemPropertyArtwork];
UIImage *resizedArtwork = [artwork resizedImage:thumbnailSize interpolationQuality:kCGInterpolationMedium];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!