如何使用 Swift 获取 UIImage 中像素的颜色? [英] How do I get the color of a pixel in a UIImage with Swift?

查看:28
本文介绍了如何使用 Swift 获取 UIImage 中像素的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Swift 获取 UIImage 中像素的颜色,但它似乎总是返回 0.这是代码,翻译自 @Minas 在 这个线程:

I'm trying to get the color of a pixel in a UIImage with Swift, but it seems to always return 0. Here is the code, translated from @Minas' answer on this thread:

func getPixelColor(pos: CGPoint) -> UIColor {
    var pixelData = CGDataProviderCopyData(CGImageGetDataProvider(self.CGImage))
    var data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)

    var pixelInfo: Int = ((Int(self.size.width) * Int(pos.y)) + Int(pos.x)) * 4

    var r = CGFloat(data[pixelInfo])
    var g = CGFloat(data[pixelInfo+1])
    var b = CGFloat(data[pixelInfo+2])
    var a = CGFloat(data[pixelInfo+3])

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

提前致谢!

推荐答案

由于我遇到了类似的问题,因此进行了一些搜索.你的代码工作正常.问题可能是从您的图像中提出的.

A bit of searching leads me here since I was facing the similar problem. You code works fine. The problem might be raised from your image.

代码:

  //On the top of your swift 
  extension UIImage {
      func getPixelColor(pos: CGPoint) -> UIColor {

          let pixelData = CGDataProviderCopyData(CGImageGetDataProvider(self.CGImage))
          let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)

          let pixelInfo: Int = ((Int(self.size.width) * Int(pos.y)) + Int(pos.x)) * 4

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

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

这个方法会从图像的 CGImage 中选择像素颜色.因此,请确保您从正确的图像中挑选.例如如果您的 UIImage 是 200x200,但来自 Imgaes.xcassets 或其来源的原始图像文件是 400x400,并且您正在选择点 (100,100),您实际上是在选择图像左上角的点,而不是中.

What happens is this method will pick the pixel colour from the image's CGImage. So make sure you are picking from the right image. e.g. If you UIImage is 200x200, but the original image file from Imgaes.xcassets or wherever it came from, is 400x400, and you are picking point (100,100), you are actually picking the point on the upper left section of the image, instead of middle.

两种解决方案:
1、使用Imgaes.xcassets中的图片,并且在1x字段中只放一张@1x图片.将@2x、@3x 留空.确保您知道图像大小,并选择一个在范围内的点.

Two Solutions:
1, Use image from Imgaes.xcassets, and only put one @1x image in 1x field. Leave the @2x, @3x blank. Make sure you know the image size, and pick a point that is within the range.

//Make sure only 1x image is set
let image : UIImage = UIImage(named:"imageName") 
//Make sure point is within the image
let color : UIColor = image.getPixelColor(CGPointMake(xValue, yValue)) 

2、按比例放大/缩小CGPoint以匹配UIImage.例如let point = CGPoint(100,100) 在上面的例子中,

2, Scale you CGPoint up/down the proportion to match the UIImage. e.g. let point = CGPoint(100,100) in the example above,

let xCoordinate : Float = Float(point.x) * (400.0/200.0)
let yCoordinate : Float = Float(point.y) * (400.0/200.0) 
let newCoordinate : CGPoint = CGPointMake(CGFloat(xCoordinate), CGFloat(yCoordinate))
let image : UIImage = largeImage
let color : UIColor = image.getPixelColor(CGPointMake(xValue, yValue)) 

我只测试了第一种方法,我正在使用它从调色板中获取颜色.两者都应该工作.快乐编码:)

I've only tested the first method, and I am using it to get a colour off a colour palette. Both should work. Happy coding :)

这篇关于如何使用 Swift 获取 UIImage 中像素的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆