如何在绘制和合成UIImage时应用比例



我有以下func选项。

extension UIImage
{
var width: CGFloat
{
return size.width
}

var height: CGFloat
{
return size.height
}

private static func circularImage(diameter: CGFloat, color: UIColor) -> UIImage
{
UIGraphicsBeginImageContextWithOptions(CGSize(width: diameter, height: diameter), false, 0)
let context = UIGraphicsGetCurrentContext()!
context.saveGState()
let rect = CGRect(x: 0, y: 0, width: diameter, height: diameter)
context.setFillColor(color.cgColor)
context.fillEllipse(in: rect)
context.restoreGState()
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}

private func addCentered(image: UIImage, tintColor: UIColor) -> UIImage
{            
let topImage = image.withTintColor(tintColor, renderingMode: .alwaysTemplate)
let bottomImage = self

UIGraphicsBeginImageContext(size)

let bottomRect = CGRect(x: 0, y: 0, width: bottomImage.width, height: bottomImage.height)
bottomImage.draw(in: bottomRect)

let topRect = CGRect(x: (bottomImage.width - topImage.width) / 2.0,
y: (bottomImage.height - topImage.height) / 2.0,
width: topImage.width,
height: topImage.height)
topImage.draw(in: topRect, blendMode: .normal, alpha: 1.0)

let mergedImage = UIGraphicsGetImageFromCurrentImageContext()!

UIGraphicsEndImageContext()

return mergedImage
}
}

它们工作正常,但我如何正确地应用UIScreen.main.scale来支持视网膜屏幕?

我看过这里做了什么,但还想不出来。

有什么想法吗?

访问UIScreen.main.scale本身有点问题,因为您只能从主线程访问它(而您通常希望在后台线程上进行更重的图像处理(。因此,我建议采用其中一种方法。

首先,你可以用代替UIGraphicsBeginImageContext(size)

UIGraphicsBeginImageContextWithOptions(size, false, 0.0)

最后一个自变量(0.0(是scale;如果指定值0.0,则比例因子设置为设备主屏幕的比例因子">

相反,如果你想在生成的UIImage上保留原始图像的比例,你可以这样做:在topImage.draw之后,而不是用UIGraphicsGetImageFromCurrentImageContext获得UIImage,而是用获得CGImage

let cgImage = context.makeImage()

然后用原始图像的比例和方向(与默认值相反(构建UIImage

let mergedImage = UIImage(
cgImage: cgImage, 
scale: image.scale, 
orientation: image.opientation)

最新更新