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

查看:1256
本文介绍了如何使用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。例如在上面的例子中让点= 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天全站免登陆