大家好!
我一直在研究这个问题,并且在我的项目中集成了几种不同的解决方案,但是它们似乎都无法正常工作。目前我使用的解决方案是从这个帖子借鉴来的。
然而,当我运行代码时,发生了两件事情:
- 像素数组仍然被初始化但未填充(全为0)
我收到了两个错误消息:
CGBitmapContextCreate:不支持的参数组合:设置CGBITMAP_CONTEXT_LOG_ERRORS环境变量以查看详细信息
和
CGContextDrawImage: invalid context 0x0. If you want to see the backtrace, please set
有什么想法吗?这是我目前为我的图像类调用的构建函数:
init?(fromImage image: UIImage!) {
let imageRef = image!.CGImage
self.width = CGImageGetWidth(imageRef)
self.height = CGImageGetHeight(imageRef)
let colorspace = CGColorSpaceCreateDeviceRGB()
let bytesPerRow = (4 * width);
let bitsPerComponent :UInt = 8
let pixels = UnsafeMutablePointer<UInt8>(malloc(width*height*4))
var context = CGBitmapContextCreate(pixels, width, height, Int(bitsPerComponent), bytesPerRow, colorspace, 0);
CGContextDrawImage(context, CGRectMake(0, 0, CGFloat(width), CGFloat(height)), imageRef)
如果能给予任何指引,对我来说都很有帮助,因为我还不太了解 CGBitmap 相关的知识。
非常感谢!
可选项
。它们只会导致运行时崩溃和错误。最好检查一个可选项
并正确地处理每种情况。 - user887210