繁体   English   中英

缩小图像的画质很低?

[英]Downscaled image is drawn in very low quality?

我的徽标图片很大(1000x500)。 我想使用此图像并将其绘制在尺寸为100x100的框架中。 这是我的方法:

self.logo = UIImageView(image: UIImage(named: "logoBig")!)
self.logo?.contentMode = UIViewContentMode.ScaleAspectFit
self.logo?.frame = CGRectMake(self.view.bounds.width/2 - 50, 50, 100, 100)
self.view.addSubview(self.logo!)

这可行。 问题在于减小图像尺寸后图像质量非常低。

如何在不降低质量的情况下将大图像绘制到较小的框架中?

尝试这样:

let logo = UIImageView(frame: CGRectMake(view.bounds.midX - 50, 50, 100, 100))
logo.contentMode = .ScaleAspectFit
logo.image = UIImage(named: "logoBig")!
view.addSubview(logo)

尝试使用这种方法

 - (UIImage*)resizeImage:(UIImage*)image scaledToSize:(CGSize)newSize{ 
    //UIGraphicsBeginImageContext(newSize);
    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
    // Pass 1.0 to force exact pixel size.
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

如果要再次调整图像的大小,请保留原始图像,然后将其用于其他尺寸。

添加注意:同样,将原始图像尺寸设置为1000x500,将其调整大小会降低图像的纵横比,因为100x100与原始图像尺寸不成比例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM