问题
I'm seeing a consistent issue with MPMediaItemArtwork in that it's returning artwork in a size different to that which I request.
The code I'm using is as follows
MPMediaItem *representativeItem = [self.representativeItems objectAtIndex:index];
MPMediaItemArtwork *artwork = [representativeItem valueForProperty:MPMediaItemPropertyArtwork];
UIImage *albumCover = [artwork imageWithSize:CGSizeMake(128.0f, 128.0f)];
This works as expected, except that the size of the returned image is always {320.0f, 320.0f}
even though I specifically asked for {128.0f, 128.0f}
and it's causing some memory issues due to the images being more than twice the size of those expected.
Has anyone else witnessed this particular issue. How did you resolve it?
Apples docs suggest this should work as I'm expecting it to rather than how it actually is
回答1:
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];
回答2:
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];
来源:https://stackoverflow.com/questions/7667631/mpmediaitemartwork-returning-wrong-sized-artwork