Ios 调整图像大小但保留硬边
我正在使用这个链接中的一段代码-,它工作得很好,但我想知道它是否可以修改以保留像素的硬边。我希望图像的大小加倍,并保持像素的硬边Ios 调整图像大小但保留硬边,ios,xcode,swift,image,image-resizing,Ios,Xcode,Swift,Image,Image Resizing,我正在使用这个链接中的一段代码-,它工作得很好,但我想知道它是否可以修改以保留像素的硬边。我希望图像的大小加倍,并保持像素的硬边 class func resizeImage(image: UIImage, newHeight: CGFloat) -> UIImage { let scale = newHeight / image.size.height let newWidth = image.size.width * scale UIGraphicsBegin
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中有类似的吗?做了一些进一步的挖掘,找到了答案- 但是在哪里呢
CGContextSetInterpolationQuality(context, kCGInterpolationHigh)
而是写
CGContextSetInterpolationQuality(context, CGInterpolationQuality.None)
您需要使用
CISampler
类(仅在iOS 9中可用),并且需要为其创建自己的自定义图像处理过滤器
您可以找到更多信息,并且受到公认答案的启发,更新为Swift 5 Swift 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()