gpt4 book ai didi

ios - 从 iPad Pro 与 iPad 获取 Pixel 颜色

转载 作者:行者123 更新时间:2023-11-30 11:40:37 25 4
gpt4 key购买 nike

我从另一篇文章中发现的以下扩展可以很好地从屏幕图像上的触摸点获取像素 RGB 值。它可以在 iPad 上运行,但不能在 iPad Pro 上运行。在专业版上,无论触摸位置如何,它都会返回相同的固定值,而不是预期的 RGBA。为什么这在专业版上不能正常工作?我怀疑与 CGBitmapInfo 设置有关,但无法从 Apple 的文档中找出答案。

extension UIImageView {
func getPixelColorAt_ext(point:CGPoint) -> (clr: UIColor, red: Int, green: Int, blue: Int) {

let pixel = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: 4)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
let context = CGContext(data: pixel, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)
context!.translateBy(x: -point.x, y: -point.y)
layer.render(in: context!)
let r = Int(pixel[0])
let g = Int(pixel[1])
let b = Int(pixel[2])
let color = UIColor(red: CGFloat(r)/255.0,
green: CGFloat(g)/255.0,
blue: CGFloat(b)/255.0,
alpha: 1.0)
pixel.deallocate(capacity: 4)
return (clr: color, red: r, green: g, blue: b)
}
}

最佳答案

问题在于释放,这在 iPad Pro 上发生得更快。通过在调用 Pixel.deallocate 之前添加 let r = Int(pixel[0]) 等来修复,如上面的更正版本所示。

关于ios - 从 iPad Pro 与 iPad 获取 Pixel 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49330026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com