图像处理 – 如何在Swift for iOS中的像素级加载和编辑位图文件?

我想在 Swift的像素级别处理图像.这个问题的答案是c: How do I access and manipulate JPEG image pixels?,但我希望看到Swift的等效来源.
这就是我在触摸位置从图像中获取颜色的方法.

我翻译了这个答案:https://stackoverflow.com/a/12579413/359578

(此示例不对nil进行任何错误检查)

func createARGBBitmapContext(inImage: CGImage) -> CGContext {
  var bitmapByteCount = 0
  var bitmapBytesPerRow = 0

  //Get image width,height
  let pixelsWide = CGImageGetWidth(inImage)
  let pixelsHigh = CGImageGetHeight(inImage)

  // Declare the number of bytes per row. Each pixel in the bitmap in this
  // example is represented by 4 bytes; 8 bits each of red,green,blue,and
  // alpha.
  bitmapBytesPerRow = Int(pixelsWide) * 4
  bitmapByteCount = bitmapBytesPerRow * Int(pixelsHigh)

  // Use the generic RGB color space.
  let colorSpace = CGColorSpaceCreateDeviceRGB()

  // Allocate memory for image data. This is the destination in memory
  // where any drawing to the bitmap context will be rendered.
  let bitmapData = malloc(bitmapByteCount)

  // Create the bitmap context. We want pre-multiplied ARGB,8-bits
  // per component. Regardless of what the source image format is
  // (CMYK,Grayscale,and so on) it will be converted over to the format
  // specified here by CGBitmapContextCreate.
  let context = CGBitmapContextCreate(bitmapData,pixelsWide,pixelsHigh,8,bitmapBytesPerRow,colorSpace,CGImageAlphaInfo.PremultipliedFirst.rawValue)

  // Make sure and release colorspace before returning
  return context!
}

func getPixelColorAtLocation(point:CGPoint,inImage:CGImageRef) -> NSColor {
  // Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa,Red,Green,Blue
  let context = self.createARGBBitmapContext(inImage)

  let pixelsWide = CGImageGetWidth(inImage)
  let pixelsHigh = CGImageGetHeight(inImage)
  let rect = CGRect(x:0,y:0,width:Int(pixelsWide),height:Int(pixelsHigh))

  //Clear the context
  CGContextClearRect(context,rect)

  // Draw the image to the bitmap context. Once we draw,the memory
  // allocated for the context for rendering will then contain the
  // raw image data in the specified color space.
  CGContextDrawImage(context,rect,inImage)

  // Now we can get a pointer to the image data associated with the bitmap
  // context.
  let data = CGBitmapContextGetData(context)
  let dataType = UnsafePointer<UInt8>(data)

  let offset = 4*((Int(pixelsWide) * Int(point.y)) + Int(point.x))
  let alpha = dataType[offset]
  let red = dataType[offset+1]
  let green = dataType[offset+2]
  let blue = dataType[offset+3]
  let color = NSColor(red: CGFloat(red)/255.0,green: CGFloat(green)/255.0,blue: CGFloat(blue)/255.0,alpha: CGFloat(alpha)/255.0)

  // Free image data memory for the context
  free(data)
  return color;
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...