0

在我的应用程序中,我有 2 个 uiimageview.one 图像视图包含用户从照片库中选择的图像或从相机中选择的图像。此图像视图的内容模式是 aspectfit,因此如果图像尺寸较小,则不会拉伸图像。还有其他图像视图包含与第一个 imageview 有边框的图像。当用户选择与 imageview 大小相似的图像时,边框是合适的位置。但是当图像尺寸较小时,边框无法正常显示。边框图像和所选照片之间存在间隙图片。我不想要那个差距。我该如何解决这个问题?请参阅附图。这里我不想要图像和框架之间的黑色间隙

4

1 回答 1

3

通过使用以下代码解决了该问题:

-(CGRect)frameForImage:(UIImage*)image inImageViewAspectFit:(UIImageView*)imageView
{
    float imageRatio = image.size.width / image.size.height;

    float viewRatio = imageView.frame.size.width / imageView.frame.size.height;

    if(imageRatio < viewRatio)
    {
        float scale = imageView.frame.size.height / image.size.height;

        float width = scale * image.size.width;

        float topLeftX = (imageView.frame.size.width - width) * 0.5;

        return CGRectMake(topLeftX, 0, width, imageView.frame.size.height);
    }
    else
    {
        float scale = imageView.frame.size.width / image.size.width;

        float height = scale * image.size.height;

        float topLeftY = (imageView.frame.size.height - height) * 0.5;

        return CGRectMake(0, topLeftY, imageView.frame.size.width, height);
    }
}
于 2012-08-27T05:06:13.347 回答