ios - CGImageCreate swift 给出错误

标签 ios swift pixels

我正在尝试将图像像素绘制到图形上下文中以迭代和打印图像的色彩空间信息值。我设法缓冲了所有原始像素值。但是我尝试使用 CGImageCreate 绘制位图图像时遇到了以下错误,也是 screenshot attached .这是我写的代码。

func createRGBAPixel(inImage: CGImageRef) -> CGContextRef {
//Image width, height

let pixelWidth = CGImageGetWidth(inImage)
let pixelHeight = CGImageGetHeight(inImage)

//Declaring number of bytes 

let bytesPerRow = Int(pixelWidth) * 4
let byteCount = bytesPerRow * Int(pixelHeight)

//RGB color space

let colorSpace = CGColorSpaceCreateDeviceRGB()

//Allocating image data

let mapData = malloc(byteCount)
let mapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue)

//Create bitmap context

let context = CGBitmapContextCreate(mapData, pixelWidth, pixelHeight, Int(8), Int(bytesPerRow), colorSpace, mapInfo.rawValue)

let imageRef = CGBitmapContextCreateImage(context)


CGContextDrawImage(context, CGRectMake(0, 0, pixelWidth, pixelHeight), imageRef)


let data = CGDataProviderCopyData(CGImageGetDataProvider(imageRef)) as! NSData
let pixels = UnsafePointer<UInt8>(data.bytes)
var newPixelsArray = [UInt8](count: byteCount, repeatedValue: 0)
let provider = CGDataProviderCreateWithData(nil , &newPixelsArray, data.length, nil)
let newImageRef = CGImageCreate(pixelWidth, pixelHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBitsPerPixel(imageRef), bytesPerRow, colorSpace, mapInfo, provider, nil, false, kCGRenderingIntentDefault)


return context!

CGImageCreate 在“kCGRenderingIntentDefault”处给出错误,提示“未解析的标识符”。我也导入了 coreGraphics,它在这个地方不接受任何值。有什么建议?

请帮忙。

最佳答案

由于 Swift SDK 对此类枚举所做的操作,您必须将 kCGRenderingIntentDefault 替换为 .defaultIntent,如下所示:

let newImageRef = CGImageCreate(pixelWidth, pixelHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBitsPerPixel(imageRef), bytesPerRow, colorSpace, mapInfo, provider, nil, false, .defaultIntent)

关于ios - CGImageCreate swift 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35520144/

相关文章:

android - 以非常快速的方式在android中逐像素绘制图像

ios - 如何在 Storyboard 中创建一个单独的 View 以编程方式包含在 UITableView 中?

ios - 无法在 swift2 中声明类扩展

ios - iOS 9 及以下版本不支持 Xcode 8 自动生成的核心数据堆栈

ios - swift 3.0 : argument labels '(_:) do not match any available overloads

swift - 持久化字符串列表属性 RealmSwift

android - 移动 Web 开发框架

ios - rx_itemsWithCellFactory 调用语法

c# - 如何找到黑色像素位置

Android:如何在不同的屏幕尺寸上使用 Canvas 方法处理绘图?