我正在用 Swift 编写一个函数来创建 vImage_CGImageFormat
来自CGImage
如下:
vImage_CGImageFormat(
bitsPerComponent: UInt32(CGImageGetBitsPerComponent(image)),
bitsPerPixel: UInt32(CGImageGetBitsPerPixel(image)),
colorSpace: CGImageGetColorSpace(image),
bitmapInfo: CGImageGetBitmapInfo(image),
version: UInt32(0),
decode: CGImageGetDecode(image),
renderingIntent: CGImageGetRenderingIntent(image))
但是这不能编译。那是因为CGImageGetColorSpace(image)
返回CGColorSpace!
而上面的构造函数只需要 Unmanaged<CGColorSpace>
对于colorSpace
参数。
还有其他方法可以做到这一点吗?也许转换CGColorSpace
进入Unmanaged<CGColorSpace>
?
最佳答案
这应该有效:
vImage_CGImageFormat(
// ...
colorSpace: Unmanaged.passUnretained(CGImageGetColorSpace(image)),
//...
)
来自struct Unmanaged<T>
API文档:
/// Create an unmanaged reference without performing an unbalanced
/// retain.
///
/// This is useful when passing a reference to an API which Swift
/// does not know the ownership rules for, but you know that the
/// API expects you to pass the object at +0.
///
/// ::
///
/// CFArraySetValueAtIndex(.passUnretained(array), i,
/// .passUnretained(object))
static func passUnretained(value: T) -> Unmanaged<T>
Swift 3 更新:
vImage_CGImageFormat(
// ...
colorSpace: Unmanaged.passUnretained(image.colorSpace!),
//...
)
关于ios - 如何从 CGColorSpace 获取 Unmanaged<CGColorSpace> ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28361530/