MPMediaItemArtwork returning wrong sized artwork
Asked Answered
R

2

7

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

Rhinarium answered 5/10, 2011 at 20:55 Comment(0)
R
10

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]; 
Rubino answered 10/10, 2011 at 14:44 Comment(7)
I filed this as a bug in Apple's bug reporter system - <rdar://10259111>Rubino
This is good to know, I thought either I was going insane or there was an issue with my device. I appreciate you filing the bug; can you update this post if you receive a response from Apple?Rhinarium
I'm already manually resizing the images but I use Matt Gemmells MGImageUtilities as when I tried the link above the resized image quality was very poor as it doesn't take into account the scale for Retina capable devices.Rhinarium
Apple did follow up with me and they asked me to try this under iOS 5. The new results is that we'll still see "55 x 55", but the returned scale will be a more expected (for Retina displays) 2.0. Are you having better results on your end as well?Rubino
Thanks for the update. I've not updated to 5.0 yet but I'm still concerned that it's not returning the specified size or if that is the desired behaviour that the docs haven't been updated to reflect that. Are you able to update the case stating if they believe that to be the desired result they need to update the docs (and preferably change the method name).Rhinarium
Yep... I told them "55 x 55" doesn't seem like a very intuitive result when we requested an image of size 30 (in my sample code). Either give me 30 or 60. Or clarify the docs. Let's see what happens.Rubino
I can report having the same issue on iOS 7.Annamarieannamese
M
0

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];
Mchenry answered 25/10, 2011 at 13:56 Comment(1)
I've mentioned in the comments to the answer that I'm already manually resizing the images, but I use Matt Gemmell's code as Trevor Harman's doesn't take into account Retina devices. However, my issue was with Apple's method not returning a image sized to what I requested, which has been raised as a bug.Rhinarium

© 2022 - 2024 — McMap. All rights reserved.