gpt4 book ai didi

ios - 如何在不降低质量的情况下放大微小的文本图像?

转载 作者:行者123 更新时间:2023-11-28 23:20:55 25 4
gpt4 key购买 nike

文字图片非常小,大小为 17px x 10px

tiny text image

在macOS下可以完美放大

enter image description here

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 中做同样的事情,我得到的图像质量很差:

enter image description here

   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
}

调整大小时如何获得更好的图像?谢谢。

最佳答案

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

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:

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))
}
}
}

关于ios - 如何在不降低质量的情况下放大微小的文本图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59692077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com