【问题标题】:Swift 4: Get RGB values of pixel in UIImageSwift 4:获取 UIImage 中像素的 RGB 值
【发布时间】:2023-03-24 01:49:01
【问题描述】:

我知道有很多关于如何在给定CGPoint 的情况下获取UIImage 中像素颜色的帖子,但据我所知,它们都已经过时了。它们中的大多数包含CGImageGetDataProviderCGDataProviderCopyData,这在 Swift 4 中是一个错误:

“CGImageGetDataProvider”已被属性“CGImage.dataProvider”替换 'CGDataProviderCopyData' 已被属性 'CGDataProvider.data' 取代

Xcode 建议使用这些替代品,但它们不存在,因此我在尝试重新创建 Swift 4 函数以获取 UIImage 中像素的颜色时遇到了麻烦。

这是典型的 Swift 3 函数:

extension UIImage {

    subscript (x: Int, y: Int) -> UIColor? {

        if x < 0 || x > Int(size.width) || y < 0 || y > Int(size.height) {
            return nil
        }

        let provider = CGImageGetDataProvider(self.cgImage!)
        let providerData = CGDataProviderCopyData(provider!)
        let data = CFDataGetBytePtr(providerData)

        let numberOfComponents = 4
        let pixelData = ((Int(size.width) * y) + x) * numberOfComponents

        let r = CGFloat(data![pixelData]) / 255.0
        let g = CGFloat(data![pixelData + 1]) / 255.0
        let b = CGFloat(data![pixelData + 2]) / 255.0
        let a = CGFloat(data![pixelData + 3]) / 255.0

        return UIColor(red: r, green: g, blue: b, alpha: a)
    }

}

非常感谢任何建议或 cmets。谢谢!


编辑

我尝试了@Mukesh 的解决方案,但即使所有构建错误都已修复,程序仍会崩溃。它说:

我给函数的图片不是nil,我查过了。


附:我用于该功能的图像是相机的快照。我有一个实时摄像机,在每一帧之后,都会调用这个函数(如下),我将当前帧转换为UIImage。这个UIImage是我要查找的像素颜色:

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
        let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!
        let ciImg = CIImage(cvPixelBuffer: pixelBuffer)
        let cameraImage = UIImage(ciImage: ciImg)
        let col = cameraImage.getPixelColor(pos: CGPoint(x: 100, y: 100))
   }

我以CGPoint(x: 100, y: 100) 为例来查看它是否崩溃,并且确实发生了。这一点也在图像中,因为如果不是,它会在这里返回nil

if x < 0 || x > Int(size.width) || y < 0 || y > Int(size.height) {
            return nil
}

有没有办法找出为什么它会获得nil 值?或者也许是不同的解决方案?谢谢你:)

【问题讨论】:

  • 你试过了吗? let provider = self.cgImage!.dataProvider let providerData = provider!.data

标签: ios swift colors uicolor


【解决方案1】:

这是我删除错误后得到的代码:

extension UIImage {

    subscript (x: Int, y: Int) -> UIColor? {

        if x < 0 || x > Int(size.width) || y < 0 || y > Int(size.height) {
            return nil
        }

        let provider = self.cgImage!.dataProvider
        let providerData = provider!.data
        let data = CFDataGetBytePtr(providerData)

        let numberOfComponents = 4
        let pixelData = ((Int(size.width) * y) + x) * numberOfComponents

        let r = CGFloat(data![pixelData]) / 255.0
        let g = CGFloat(data![pixelData + 1]) / 255.0
        let b = CGFloat(data![pixelData + 2]) / 255.0
        let a = CGFloat(data![pixelData + 3]) / 255.0

        return UIColor(red: r, green: g, blue: b, alpha: a)
    }
}

【讨论】:

  • 感谢您的回复,但即使错误消失,程序也会崩溃。阅读我的帖子的编辑,谢谢!
  • 您好,您能否检查一下我在编辑您的程序时遇到的错误。也许你可以看看你是否也发生了错误,谢谢
  • 您好,很抱歉再次打扰您,但只是想知道,UIImage 的点使用什么坐标系?是 UIKit - 例如点 (0,0) 在左上角?谢谢!
  • UIImage 和 UIView 使用左上角作为原点,Core Image 和 Core Graphics 使用左下角。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-10
  • 2020-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-24
  • 1970-01-01
相关资源
最近更新 更多