我使用的是Xcode 6.3.1和Swift。
当一个具有多个参数的函数在参数类型上出现错误时,很难知道哪个参数是错误的。
例如,CGBitmapContextCreate()
,这段代码:
let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue)
let context = CGBitmapContextCreate(nil, UInt(size.width), UInt(size.height), 8, 0, colorSpace, bitmapInfo)
将产生如下错误:
MyFile.swift:23:19: Cannot invoke 'CGBitmapContextCreate' with an argument list of type '(nil, UInt, UInt, Int, Int, CGColorSpace, CGBitmapInfo)'
通过仔细比较文档和我的参数列表,我可以发现它是第二个和第三个参数,应该是Int。
有没有办法让编译器在这方面更智能呢?
发布于 2015-07-01 23:49:34
问题可能是,根据您在编译时实际访问的定义,您正在查找的CGBitmapContextCreate
在线文档是错误的。
最后一个参数需要是UInt32
类型,并且CGBitmapInfo
将返回一个CGBitmapInfo
对象。这就是编译器出错的原因。您传入的参数类型不正确。你甚至可以右击函数并点击“查看定义”,这将验证我所说的话。
尝试一下,直接传入CGImageAlphaInfo.PremultipliedLast.rawValue
,因为它是正在查找的UInt32。
示例解决方案:
let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGImageAlphaInfo.PremultipliedLast.rawValue
let context = CGBitmapContextCreate(nil, UInt(size.width), UInt(size.height), 8, 0, colorSpace, bitmapInfo)
你会发现你将能够编译源代码,并得到预期的结果。请注意,您仍然可以对该值应用任何想要的按位操作。
PS:我遇到了和你一样的问题,当我找不到解决方案时,我感到非常沮丧。
发布于 2015-09-28 20:13:50
它起作用了!
let width = CGImageGetWidth(image)
let height = CGImageGetHeight(image)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bytesPerRow = 4 * width;
let bitsPerComponent :Int = 8
let pixels = UnsafeMutablePointer<UInt8>(malloc(width*height*4))
var context = CGBitmapContextCreate(pixels, width, height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapInfo())
https://stackoverflow.com/questions/30124271
复制相似问题