【问题标题】:resizing image with appropriate aspect ratio以适当的纵横比调整图像大小
【发布时间】:2012-03-05 13:41:47
【问题描述】:

我正在开发新应用程序,其中我有不同大小的图像。我用不同的解决方案尝试了很多次,但找不到合适的图像纵横比。我也想显示高质量的图像。有一些图像的分辨率为 20*20 到 3456 * 4608,或者可能非常高。

所有图片都将在不裁剪的情况下显示,但我们的应用屏幕尺寸为 300 * 370 pxls。

我遵循以下参考:

1:Resize UIImage with aspect ratio?

2:UIViewContentModeScaleAspectFit

3:scale Image in an UIButton to AspectFit?

4:Resize UIImage with aspect ratio?

【问题讨论】:

  • 您不能只获取屏幕的宽度/高度比率,然后将其应用于您必须显示的图像吗?

标签: iphone ipad


【解决方案1】:

我用于将图像大小调整到最多 1000 像素边缘的代码是这样的:

const int maxPhotoWidthOrHeight = 1000;

- (UIImage*)resizeImageWithImage:(UIImage*)image
{
    CGFloat oldWidth = image.size.width;
    CGFloat oldHeight = image.size.height;
    NSLog(@"Old width %f , Old Height : %f ",oldWidth,oldHeight);

    if (oldHeight > maxPhotoWidthOrHeight || oldWidth > maxPhotoWidthOrHeight) {//resize if necessary
        CGFloat newHeight;
        CGFloat newWidth;
        if (oldHeight > oldWidth ) {
            newHeight = maxPhotoWidthOrHeight;
            newWidth = oldWidth * (newHeight/oldHeight);
        }
        else{
            newWidth = maxPhotoWidthOrHeight;
            newHeight = oldHeight * (newWidth/oldWidth);
        }
        CGSize newSize = CGSizeMake(newWidth, newHeight);
        UIGraphicsBeginImageContext( newSize );
        [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        NSLog(@"New width %f , New Height : %f ",newImage.size.width,newImage.size.height);

        return newImage;
    }
    else{//do not resize because both edges are less than 1000px
        return image;
    }

}

【讨论】:

    【解决方案2】:

    --请添加您的代码示例,否则很难为您的问题提供任何解决方案。

    这里找到的解决方案也是How to scale a UIImageView proportionally?

    imageView.contentMode = UIViewContentModeScaleAspectFit;
    

    【讨论】:

      猜你喜欢
      • 2017-08-10
      • 2013-06-26
      • 2012-04-15
      • 1970-01-01
      • 2012-05-01
      • 2013-09-09
      相关资源
      最近更新 更多