如何在不损失画质的情况下放大微小的文字图像? [英] How to enlarge a tiny text image without loosing quality?

查看:51
本文介绍了如何在不损失画质的情况下放大微小的文字图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文本图像非常小,尺寸为17px x 10px

The text image is very tiny, 17px x 10px in size

它可以在MacOS中完美放大

It can be enlarge perfectly in MacOS

let width  = scale * image.size.width * kScale
        let height = scale * image.size.height * kScale
        super.init(frame: NSRect(x: 0, y: 0, width: width, height: height))
        self.makeConstraints(width: width, height: height)
        self.drawBlock = { (context, bounds) in
            image.draw(in: bounds)
        }

并重新绘制它:

   override func draw(_ dirtyRect: NSRect) {
        // Fill with optional background color.

        if let color = bgColor {
            color.set()
            bounds.fill(using: .sourceOver)
        }

        // Draw with optional draw block.

        if let block = drawBlock {
            let context = NSGraphicsContext.current!.cgContext
            block(context, bounds)
        }

        super.draw(dirtyRect)
    }

我尝试使用以下两种方法在iOS中进行同样的操作,但得到的图像质量很低:

I tried to the same thing in iOS with the following two methods, I got an image with low quality:

   func imageScaledToSize(image: UIImage, size : CGSize) -> UIImage{
        UIGraphicsBeginImageContextWithOptions(size, false, 0.0)

        let imageRect = CGRect(origin: .zero, size: size)
        image.draw(in: imageRect)

        let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return scaledImage!
    }

   func resizeImage(image: UIImage, newSize: CGSize) -> (UIImage) {

       let newRect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height).integral
       UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
       let context = UIGraphicsGetCurrentContext()

       // Set the quality level to use when rescaling
        context!.interpolationQuality = CGInterpolationQuality.high
       let flipVertical = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: newSize.height)

       context!.concatenate(flipVertical)
       // Draw into the context; this scales the image
       context?.draw(image.cgImage!, in: CGRect(x: 0.0,y: 0.0, width: newRect.width, height: newRect.height))

       let newImageRef = context!.makeImage()! as CGImage
       let newImage = UIImage(cgImage: newImageRef)

       // Get the resized image from the context and a UIImage
       UIGraphicsEndImageContext()

       return newImage
    }

调整大小后如何获得更好的图像?谢谢.

How to get a better image when resize it? Thanks.

推荐答案

在绘制到上下文之前,您需要将上下文插值设置为无.

You need to set your context interpolation to none before drawing to your context:

extension UIImage {
    func resized(toWidth width: CGFloat, interpolationQuality: CGInterpolationQuality = .none) -> UIImage? {
        let canvasSize = CGSize(width: width, height: CGFloat(ceil(width/size.width * size.height)))
        UIGraphicsBeginImageContextWithOptions(canvasSize, false, scale)
        defer { UIGraphicsEndImageContext() }
        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        context.interpolationQuality = interpolationQuality
        draw(in: CGRect(origin: .zero, size: canvasSize))
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}

对于iOS 10或更高版本,您可以按以下方式使用 UIGraphicsImageRenderer :

For iOS 10 or later you can use UIGraphicsImageRenderer as follow:

extension UIImage {
    func resized(toWidth width: CGFloat, interpolationQuality: CGInterpolationQuality = .none, isOpaque: Bool = false) -> UIImage? {
        let canvas = CGSize(width: width, height: CGFloat(ceil(width/size.width * size.height)))
        let format = imageRendererFormat
        format.opaque = isOpaque
        return UIGraphicsImageRenderer(size: canvas, format: format).image { context in
            context.cgContext.interpolationQuality = interpolationQuality
            draw(in: CGRect(origin: .zero, size: canvas))
        }
    }
}

这篇关于如何在不损失画质的情况下放大微小的文字图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆