我正在使用此链接中的一段代码 - Resize UIImage by keeping Aspect ratio and width,它运行正常,但我想知道是否可以更改它以保留像素的硬边缘。我希望将图像的大小加倍并保持像素的硬边缘。
class func resizeImage(image: UIImage, newHeight: CGFloat) -> UIImage {
let scale = newHeight / image.size.height
let newWidth = image.size.width * scale
UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight))
image.drawInRect(CGRectMake(0, 0, newWidth, newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
在Photoshop中,调整大小时会有最近邻居插值,在iOS中有类似内容吗?
答案 0 :(得分:2)
受接受的答案启发,已更新为Swift 5。
快速5
let image = UIImage(named: "Foo")!
let scale: CGFloat = 2.0
let newSize = image.size.applying(CGAffineTransform(scaleX: scale, y: scale))
UIGraphicsBeginImageContextWithOptions(newSize, false, UIScreen.main.scale)
let context = UIGraphicsGetCurrentContext()!
context.interpolationQuality = .none
let newRect = CGRect(origin: .zero, size: newSize)
image.draw(in: newRect)
let newImage = UIImage(cgImage: context.makeImage()!)
UIGraphicsEndImageContext()
答案 1 :(得分:1)
进行了更多挖掘并找到答案 -
https://stackoverflow.com/a/25430447/4196903
但是
CGContextSetInterpolationQuality(context, kCGInterpolationHigh)
改为写
CGContextSetInterpolationQuality(context, CGInterpolationQuality.None)
答案 2 :(得分:0)