我正在尝试覆盖人字形按钮,该按钮将允许用户关闭当前视图。人字形的颜色在深色图像上应该是浅色,在深色图像上应该是深色。我已经附上了我正在描述的屏幕截图。
但是,在尝试计算图像的明暗度时,会对性能产生重大影响(我正在这样做(在CGImage上操作):
var isDark: Bool {
guard let imageData = dataProvider?.data else { return false }
guard let ptr = CFDataGetBytePtr(imageData) else { return false }
let length = CFDataGetLength(imageData)
let threshold = Int(Double(width * height) * 0.45)
var darkPixels = 0
for i in stride(from: 0, to: length, by: 4) {
let r = ptr[i]
let g = ptr[i + 1]
let b = ptr[i + 2]
let luminance = (0.299 * Double(r) + 0.587 * Double(g) + 0.114 * Double(b))
if luminance < 150 {
darkPixels += 1
if darkPixels > threshold {
return true
}
}
}
return false
}
例如,如果人字形下方的特定区域较暗,但图像的其余部分较浅,则效果不佳。
我想只对图像的一小部分进行计算,因为人字形非常小。我尝试使用CGImage的cropping(to rect: CGRect)
裁剪图像,但是挑战在于图像设置为长宽比填充,这意味着UIImageView框架的顶部不是UIImage的顶部(例如,图像可能会被放大并居中)。在通过宽高比调整图像后,是否可以仅将出现在人字形框下方的图像部分隔离出来?
修改
感谢已接受答案中的第一个链接,我得以实现这一目标。我创建了一系列扩展程序,我认为应该适用于我的其他情况。
extension UIImage {
var isDark: Bool {
return cgImage?.isDark ?? false
}
}
extension CGImage {
var isDark: Bool {
guard let imageData = dataProvider?.data else { return false }
guard let ptr = CFDataGetBytePtr(imageData) else { return false }
let length = CFDataGetLength(imageData)
let threshold = Int(Double(width * height) * 0.45)
var darkPixels = 0
for i in stride(from: 0, to: length, by: 4) {
let r = ptr[i]
let g = ptr[i + 1]
let b = ptr[i + 2]
let luminance = (0.299 * Double(r) + 0.587 * Double(g) + 0.114 * Double(b))
if luminance < 150 {
darkPixels += 1
if darkPixels > threshold {
return true
}
}
}
return false
}
func cropping(to rect: CGRect, scale: CGFloat) -> CGImage? {
let scaledRect = CGRect(x: rect.minX * scale, y: rect.minY * scale, width: rect.width * scale, height: rect.height * scale)
return self.cropping(to: scaledRect)
}
}
extension UIImageView {
func hasDarkImage(at subsection: CGRect) -> Bool {
guard let image = image, let aspectSize = aspectFillSize() else { return false }
let scale = image.size.width / frame.size.width
let cropRect = CGRect(x: (aspectSize.width - frame.width) / 2,
y: (aspectSize.height - frame.height) / 2,
width: aspectSize.width,
height: frame.height)
let croppedImage = image.cgImage?
.cropping(to: cropRect, scale: scale)?
.cropping(to: subsection, scale: scale)
return croppedImage?.isDark ?? false
}
private func aspectFillSize() -> CGSize? {
guard let image = image else { return nil }
var aspectFillSize = CGSize(width: frame.width, height: frame.height)
let widthScale = frame.width / image.size.width
let heightScale = frame.height / image.size.height
if heightScale > widthScale {
aspectFillSize.width = heightScale * image.size.width
}
else if widthScale > heightScale {
aspectFillSize.height = widthScale * image.size.height
}
return aspectFillSize
}
}
答案 0 :(得分:1)
这里有几个选项可用于将图像调整为适合视图的尺寸:How to know the image size after applying aspect fit for the image in an UIImageView
掌握了这些信息后,就可以弄清楚人字形的位置(您可能需要先转换其帧https://developer.apple.com/documentation/uikit/uiview/1622498-convert)
如果仍然缺乏性能,我会考虑使用CoreImage进行计算:https://www.hackingwithswift.com/example-code/media/how-to-read-the-average-color-of-a-uiimage-using-ciareaaverage
使用CoreImage可以有几种方法,但是获取平均值是最简单的。