Текстовое изображение очень маленькое, размером 17px x 10px
В 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 с помощью следующих двух методов, я получил изображение с низким качеством:
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
}
Как получить лучшее изображение при изменении его размера? Спасибо.