Swift 空CGContext

Swift 空CGContext,swift,core-graphics,Swift,Core Graphics,在Objective-C中,我能够使用CGBitmapContextCreate创建一个空上下文。我试图在Swift 3中实现同样的效果,但出于某种原因,它是零。我错过了什么 let inImage: UIImage = ... let width = Int(inImage.size.width) let height = Int(inImage.size.height) let bitmapBytesPerRow = width * 4 let bitmapByteCount = bitm

在Objective-C中,我能够使用
CGBitmapContextCreate
创建一个空上下文。我试图在Swift 3中实现同样的效果,但出于某种原因,它是零。我错过了什么

let inImage: UIImage = ...
let width = Int(inImage.size.width)
let height = Int(inImage.size.height)

let bitmapBytesPerRow = width * 4
let bitmapByteCount = bitmapBytesPerRow * height

let pixelData = UnsafeMutablePointer<UInt8>.allocate(capacity: bitmapByteCount)

let context = CGContext(data: pixelData,
                                width: width,
                                height: height,
                                bitsPerComponent: 8,
                                bytesPerRow: bitmapBytesPerRow,
                                space: CGColorSpaceCreateDeviceRGB(),
                                bitmapInfo: CGImageAlphaInfo.alphaOnly.rawValue)
让inImage:UIImage=。。。
let width=Int(inImage.size.width)
let height=Int(inImage.size.height)
让bitmapBytesPerRow=宽度*4
设bitmapByteCount=bitmapBytesPerRow*高度
让pixelData=Unsafemeutablepointer.allocate(容量:bitmapByteCount)
让context=CGContext(数据:pixelData,
宽度:宽度,
高度:高度,,
比特组件:8,
bytesPerRow:bitmapBytesPerRow,
空格:CGColorSpaceCreateDeviceRGB(),
bitmapInfo:CGImageAlphaInfo.alphaOnly.rawValue)

我不确定喜欢的文章中的代码会做什么,但您的Swift代码有两个不同之处

  • bytesPerRow:width//width*4(=bitmapBytesPerRow)
  • space:NULL//CGColorSpaceCreateDeviceRGB()
没有说明为
colorspace
提供
NULL
,但是标题文档说每个像素的组件数量由
space
指定,因此至少,
cgcolorspace创建设备gb()
不适用于
alphaOnly
(每个像素应该只有一个组件)

据我测试,此代码返回非nil
CGContext

    let bitmapBytesPerRow = width //<-
    let bitmapByteCount = bitmapBytesPerRow * height

    let pixelData = UnsafeMutablePointer<UInt8>.allocate(capacity: bitmapByteCount)

    let context = CGContext(data: pixelData,
                            width: width,
                            height: height,
                            bitsPerComponent: 8,
                            bytesPerRow: bitmapBytesPerRow,
                            space: CGColorSpaceCreateDeviceGray(), //<-
                            bitmapInfo: CGImageAlphaInfo.alphaOnly.rawValue)

让bitmapBytesPerRow=width/我不确定喜欢的文章中的代码会做什么,但是您的Swift代码有两个不同之处

  • bytesPerRow:width//width*4(=bitmapBytesPerRow)
  • space:NULL//CGColorSpaceCreateDeviceRGB()
没有说明为
colorspace
提供
NULL
,但是标题文档说每个像素的组件数量由
space
指定,因此至少,
cgcolorspace创建设备gb()
不适用于
alphaOnly
(每个像素应该只有一个组件)

据我测试,此代码返回非nil
CGContext

    let bitmapBytesPerRow = width //<-
    let bitmapByteCount = bitmapBytesPerRow * height

    let pixelData = UnsafeMutablePointer<UInt8>.allocate(capacity: bitmapByteCount)

    let context = CGContext(data: pixelData,
                            width: width,
                            height: height,
                            bitsPerComponent: 8,
                            bytesPerRow: bitmapBytesPerRow,
                            space: CGColorSpaceCreateDeviceGray(), //<-
                            bitmapInfo: CGImageAlphaInfo.alphaOnly.rawValue)

让bitmapBytesPerRow=width/我在做这件事时遇到了同样的问题。
我找到的解决办法是使用

var colorSpace =   CGColorSpace.init(name: CGColorSpace.sRGB)!
  let context = CGContext(data: nil,
                            width: Int(outputSize.width),
                            height: Int(outputSize.height),
                            bitsPerComponent: self.bitsPerComponent,
                            bytesPerRow: bitmapBytesPerRow,
                            space: colorSpace,
                            bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)
实际上,我的图像的颜色空间被索引了,不能用来创建上下文。 因此,我没有使用图像自己的颜色空间,而是使用

var colorSpace =   CGColorSpace.init(name: CGColorSpace.sRGB)!
并将其传递给上下文。
它解决了我的错误(无上下文问题)。

我在做这件事时遇到了同样的问题。 我找到的解决办法是使用

var colorSpace =   CGColorSpace.init(name: CGColorSpace.sRGB)!
  let context = CGContext(data: nil,
                            width: Int(outputSize.width),
                            height: Int(outputSize.height),
                            bitsPerComponent: self.bitsPerComponent,
                            bytesPerRow: bitmapBytesPerRow,
                            space: colorSpace,
                            bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)
实际上,我的图像的颜色空间被索引了,不能用来创建上下文。 因此,我没有使用图像自己的颜色空间,而是使用

var colorSpace =   CGColorSpace.init(name: CGColorSpace.sRGB)!
并将其传递给上下文。
它解决了我的错误(无上下文问题)。

测试了您的代码,Xcode在控制台中说CGBitmapContextCreate:不支持的参数组合。为什么指定
CGImageAlphaInfo.alphaOnly.rawValue
(没有颜色数据,只有alpha通道。)我相信即使在Objective-C中使用该参数,您也会得到零。我正在尝试将此代码重写为Swift:。任何CGImageAlphaInfo值都失败。测试了您的代码,Xcode在控制台中显示CGBitmapContextCreate:不支持的参数组合。为什么指定
CGImageAlphaInfo.alphaOnly.rawValue
(没有颜色数据,只有alpha通道。)我相信,即使在Objective-C中使用该参数,您也会得到nil。我正在尝试将此代码重写为Swift:。任何CGImageAlphaInfo值都失败。感谢帮助谢谢帮助谢谢帮助!我对其进行了一点修改。我删除了colorSpace变量,并将调用放在了CGColorSpace.init()上直接在space:选项中,因为我以后不需要该属性。我还将context属性声明为类变量,以便以后可以在类方法中实际分配正确的CGContext。做得很好!谢谢!我对它进行了一点修改。我删除了colorSpace变量,并调用了CGColorSpace.init()直接在空格中:选项,因为我以后不需要该属性。我还将context属性声明为类变量,以便以后可以在类方法中实际分配正确的CGContext。干得好!