【问题标题】:Reduce Resolution UIImage/UIImageView [duplicate]降低分辨率 UIImage/UIImageView [重复]
【发布时间】:2013-05-20 21:43:30
【问题描述】:

我创建了一个包含高分辨率图像的 UIImageView。问题是该图像的分辨率太高而无法放入 imageView。 imageView 的大小是 92 x 91(所以它很小)。它包含一个 UIImage,其分辨率太高,因此在 UIImageView 中看起来很难看。 那么如何降低 UIImage 的分辨率呢?

我的 UIImageView 代码:

UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:pngFilePath]];
    myImageView.frame = CGRectMake(212.0, 27, 92,91);

【问题讨论】:

  • 您对分辨率的定义是什么?它通常用作每单位的像素数。听起来您将其定义为图像的尺寸。

标签: ios objective-c


【解决方案1】:

看看这个

https://stackoverflow.com/a/2658801

这将帮助您根据需要调整图像大小

将方法添加到您的代码中并像这样调用

UIImage *myImage =[UIImage imageWithContentsOfFile:pngFilePath];
UIImage *newImage =[UIImage imageWithImage:myImage scaledToSize:CGSizeMake(92,91)];

【讨论】:

  • 如何实现该类方法?
  • 请在更新后的答案中找到
【解决方案2】:

您可以使用此方法调整图像的大小,该方法返回一个调整大小的图像:

 -(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    // Here pass new size you need
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}

希望对你有帮助。

【讨论】:

  • 如何用我的 UIImage 实现这段代码?
【解决方案3】:

您需要缩小图像尺寸,这将降低分辨率,使其更易于存储。我实际上写了一个类(基于 SO 的一些东西)就是这样做的。在我的github上,看一下:

https://github.com/pavlovonline/UIImageResizer

主要方法是

-(UIImage*)resizeImage:(UIImage*)image toSize:(CGFloat)size

所以你给这个方法你想要缩小你的图像的大小。如果高度大于宽度,它将自动计算中间并为您提供一个完美居中的正方形。宽度大于高度也是如此。如果您需要非正方形的图像,请自行调整。

所以你会得到一个缩小的 UIImage,然后你可以把它放到你的 UIImageView 中。也节省一些内存。

【讨论】:

    【解决方案4】:

    试试这个

         myImageView.contentMode = UIViewContentModeScaleToFill;
    

    【讨论】:

    • 这到底是怎么回答这个问题的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多