【问题标题】:How to set MediaItemArtwork Image Fill in UITableViewCell in iOS?如何在 iOS 中的 UITableViewCell 中设置 MediaItemArtwork 图像填充?
【发布时间】:2013-01-31 14:26:40
【问题描述】:

我正在尝试使用以下代码将MPMediaItemArtwork Image 输入UITableView's 单元格的 ImageView。

MPMediaItemArtwork *artwork = [[[self.arrayOfAlbums objectAtIndex:indexPath.row] representativeItem]valueForProperty:MPMediaItemPropertyArtwork];
    UIImage *artworkImage = [artwork imageWithSize: cell.imageView.bounds.size];

if (artworkImage)
    {
        cell.imageView.image = artworkImage;

    }

    else
    {
        cell.imageView.image = [UIImage imageNamed: @"noArtwork.png"];
    }

当我在UITableView's 单元格ImageView 中插入Artwork 图像时没问题。

但是当我的作品图像太小或太大时,就像下图一样。 没有完全填满cell的ImageView。

你看到了吗?我想像 iOS Music App 一样设置 Stretch 填充

这是设置完全填充Stretch的内置应用艺术作品图片

我想那样做。

所以我使用了cell.imageView.contentMode = UIViewContentModeScaleAspectFill;,但它没有效果。

那我该怎么做呢? 谢谢你的工作。

【问题讨论】:

    标签: ios cocoa-touch uitableview mpmediaitem artwork


    【解决方案1】:

    试试这个来计算一个新的和合适的图像; 将newRect 调整为单元格的矩形。

    // scale and center the image
    
    CGSize sourceImageSize = [artworkImage size];
    
    // The rectangle of the new image
    CGRect newRect;
    newRect = CGRectMake(0, 0, 40, 33);
    
    // Figure out a scaling ratio to make sure we maintain the same aspect ratio
    float ratio = MAX(newRect.size.width / sourceImageSize.width, newRect.size.height / sourceImageSize.height);
    
    UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 1.0);
    
    // Center the image in the thumbnail rectangle
    CGRect projectRect;
    projectRect.size.width = ratio * sourceImageSize.width;
    projectRect.size.height = ratio * sourceImageSize.height;
    projectRect.origin.x = (newRect.size.width - projectRect.size.width) / 2.0;
    projectRect.origin.y = (newRect.size.height - projectRect.size.height) / 2.0;
    
    [sourceImage drawInRect:projectRect];
    UIImage *sizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    cell.imageView.image = sizedImage;
    

    【讨论】:

    • 太赫兹为你的答案兄弟。太完美了:)
    【解决方案2】:

    线

    UIImage *artworkImage = [artwork imageWithSize: cell.imageView.bounds.size];
    

    创建一个具有单元格大小的图像。所以,它在这条线被缩放,图像不会比单元格大,因此它不会在之后缩放。

    我会将图像保留为原始大小或单元格大小的 2 倍,并将缩放比例保留为 contentMode

    CGSize newSize = CGSizeMake(artwork.bounds.size.width, artwork.bounds.size.height)
    UIImage *artworkImage = [artwork imageWithSize: newSize];
    

    【讨论】:

    • thz 。但是当我的作品图像很大时。拉伸太多了。我该怎么做?我想做同样大小的艺术品图像。
    • 我不明白,你的意思。什么大小一样?什么是“拉伸太多”?
    • 对不起。我的意思是,当我使用您的代码编写时,有些艺术品太大并且即使到达单元格的中心也拉伸太多。我的意思是即使我的作品太大,我也想像 iOS 内置应用程序一样缩放尺寸。请查看 iOS 音乐应用的专辑。它以完全相同的尺寸显示所有艺术品。甚至不会超出 cell.imageView 的大小。像那个兄弟。我怎么能这样做?
    • 您是否设置了 contentMode(在代码中或在 Interface Builder 中)?达到哪个值?
    • 是的,我的代码 cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-18
    相关资源
    最近更新 更多