【问题标题】:imageWithSize Flakey with MPMediaItemArtwork in iOS8iOS8 中带有 MPMediaItemArtwork 的 imageWithSize Flakey
【发布时间】:2014-10-09 19:06:12
【问题描述】:

在我的应用中,我在一个按钮上显示专辑插图。在 iPhone 5 上,按钮边界大小为 273x269.5。我调整艺术品大小的代码一直非常简单,没有任何改变。

MPMediaItem *currentItem = [musicPlayer nowPlayingItem];
MPMediaItemArtwork *iTunesArtwork = [currentItem valueForProperty: MPMediaItemPropertyArtwork];

CGSize buttonBounds = self.albumArtworkButton.bounds.size;
UIImage *resizedImage = [iTunesArtwork imageWithSize: CGSizeMake (buttonBounds.width, buttonBounds.height)];

突然在 iOS8 中,执行这个会导致 resizedImage = nil。真正奇怪的是,如果我更改最后一行,则执行以下行:

UIImage *resizedImage = [iTunesArtwork imageWithSize: CGSizeMake (300, 300)];

产生一个有效的图像(即 resizedImage 不是 nil 并且可以显示图像)。

任何想法可能导致这种情况?我没有改变任何东西,但是代码坏了。

【问题讨论】:

    标签: uiimage ios8 mpmediaitem


    【解决方案1】:

    我的应用程序也在 iOS8 中看到了这个新错误。这只发生在一些封面上,这取决于我的目标大小。似乎该错误与它无法通过某种因素减少图像有关。例如,我有一些来自 iTunes 的 600x600 图像,当我尝试获得 100x100 的 imageWithSize 时,它​​在尝试获得 200x200 的 imageWithSize 时返回 nil。我有一张 200x203 和 100x100 不起作用的图像,101x101 不起作用,但 102x102 会起作用 - 我认为您想要的最高分辨率可能需要超过原始尺寸的 1/2 才能避免错误,但是根据在 200x200 下工作的 600x600 图像,这并不是真的。也许它的总内存大小变化或类似的东西 - 无论如何,当你没有它们的源时很难弄清楚为什么会发生错误,所以我停止猜测并使用了这个解决方法。当您通过 imageWithSize 请求 UIImage 时,基本上只使用 MPMediaItemArtwork 对象的原始边界。然后确保与 UIImage 一起使用的 UIImageView 的 contentMode 设置为正确显示图像(UIViewContentModeScaleAspectFill、UIViewContentModeScaleAspectFit、UIViewContentModeScaleToFill)。

    // display album artwork
    MPMediaItemArtwork* artwork = [representativeItem valueForProperty:MPMediaItemPropertyArtwork];
    CGRect bounds = [artwork bounds];
    UIImage* artworkImage = [artwork imageWithSize:bounds.size];
    if (artworkImage)
    {
        [cell.imageView setImage:artworkImage];
    }
    else
    {
        [cell.imageView setImage:[UIImage imageNamed:@"AlbumDefault.png"]];
    }
    

    【讨论】:

    • 在 imageView 上设置 contentMode 并不能解决我的问题。你还有什么秘诀吗?不幸的是,手动调整原始大小的图像非常慢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 2014-05-20
    • 2014-12-12
    • 1970-01-01
    相关资源
    最近更新 更多