当前位置: 首页 > 面试题库 >

如何在iOS的Swift中以像素级别加载和编辑位图文件?

秦博延
2023-03-14
问题内容

我想在Swift中以像素级别处理图像。针对目标c回答了此问题:如何访问和处理JPEG图像像素?,但我希望看到Swift的等效资源。


问题答案:

这就是我从触摸位置的图像中获取颜色的方式。

(此示例没有错误检查是否为零)

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;
}


 类似资料:
  • 问题内容: 我环顾四周,但未能弄清楚如何获取文本,将其覆盖在图像上,然后将两者合并为一个。 我已经用我能想到的搜索字词用尽了Google,因此,如果有人有解决方案或至少有一个提示,他们可以指出它,将不胜感激。 问题答案: 好吧…我知道了: 要调用它,您只需传递一个图像: 最初的目标是创建一个动态图像,我可以在其中使用该图像,例如在地图上的给定位置标价,这很适合。希望这对尝试做同样事情的人有所帮助。

  • 问题内容: 我有一个用代码创建的(没有): 在另一堂课中,我有一个扩展方法来下载图像异步: 我在其他地方使用了此功能并正常工作,根据我的日志,我了解到下载的图像没有问题(渲染单元时),下载图像后,单元UI未更新。 我也尝试使用像Haneke这样的缓存库,但是问题存在并且没有改变。 请帮助我理解错误 谢谢 问题答案: 设置图像后,您应该致电 编辑:从改正为

  • 我正在尝试从可执行JAR文件加载图像。 我跟踪了这里的信息,然后是这里的信息。 这是检索图像的函数: 这就是它的名称: 这是一个简单的对象。我也不会得到NullPointerException,除非它被UI屏蔽。 我检查了JAR文件,图像位于: /图片/标识1。巴布亚新几内亚 当前代码在eclipse中以及导出到JAR并在终端中运行时都有效,但在双击JAR时不起作用,在这种情况下,图标是默认的JF

  • 如何加载图像从文件夹以编程方式(如图所示)? 我使用的是Android Studio 1.2.1。

  • 问题内容: 主要代号 2.MovieAdapter.java //我可以成功显示除图片以外的所有数据 //图片链接带有“ posterpath”键 //告诉我如何在MovieAdapter的Viewholder中加载图像 // currentmovie只是一个getter和setter类 3.当前电影 问题答案: 我正在使用该库并通过以下方式加载图像 首先通过创建显示选项对象 然后通过以下方式初始

  • 我想编辑.doc(word)文档的标题。下面是我编写的代码: 但它不工作,在执行下面的代码后,它无法打开显示错误的新文档。另外,我需要在文件中提供的框中添加pic。有没有人知道怎么做? 下面是我也尝试过的链接: 用java替换word文档模板中的变量 获取相同错误: