MPMediaItemArtwork returns an image of the wrong size

I see a constant problem with MPMediaItemArtwork in that it returns the work in a size different from the one I request.

The code I use 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} , although I specifically asked for {128.0f, 128.0f} , and this caused some memory problems due to the fact that the images were more than twice as expected.

Has anyone else seen this particular problem. How did you resolve it?

Apple's docs suggest that this should work as I expect, not as it really is

+6
source share
2 answers

I downloaded Apple's AddMusic sample source, which also uses MPMediaItemArtwork to see how they handle things.

In this 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 a 55 x 55 image at a scale of 1.0.

I would say that MPMediaItemArtwork that does not meet the requested size parameters is an error that you should send via bugreporter.apple.com, although Apple may also have an excuse that "55 x 55" is some optimal size, which will be Display on iPad and iPhone.

To reduce the size of UIImage, I recommend using the Trevor Harman "UIImage + Resize" methods found here: http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way

And as soon as you add extensions of your category to your project, you can make the desired memory 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]; 
+9
source

Using Trevor Harman's “UIImage + Resize” category, simply add the resize category to MPMediaItemArtwork to get the resized image for a specific 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 

So just call

 CGSize thumbnailSize = CGSizeMake(128.0, 128.0); MPMediaItemArtwork *artwork = [myMediaItem valueForProperty:MPMediaItemPropertyArtwork]; UIImage *resizedArtwork = [artwork resizedImage:thumbnailSize interpolationQuality:kCGInterpolationMedium]; 
0
source

Source: https://habr.com/ru/post/898704/


All Articles