我有以下問題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以支持視網膜螢屏?
我已經看過這里做了什么,但還沒有弄清楚。
有任何想法嗎?
uj5u.com熱心網友回復:
訪問UIScreen.main.scale本身有點問題,因為您只能從主執行緒訪問它(而您通常希望在后臺執行緒上進行更重的影像處理)。所以我建議使用這些方法之一。
首先,您可以替換UIGraphicsBeginImageContext(size)為
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
最后一個引數 ( 0.0) 是 a scale,并且基于檔案“如果您指定的值為 0.0,則比例因子將設定為設備主螢屏的比例因子。”
如果你想在結果上保留原始影像的比例UIImage,你可以這樣做: after topImage.draw,而不是獲取 UIImage with UIGraphicsGetImageFromCurrentImageContext,獲取 CGImage with
let cgImage = context.makeImage()
然后使用原始影像的比例和方向(而不是默認值)構造 UIImage
let mergedImage = UIImage(
cgImage: cgImage,
scale: image.scale,
orientation: image.opientation)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/354489.html
標籤:ios 迅速 用户界面 规模 uigraphicscontext
