我正在用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>
发布于 2015-02-06 08:49:41
这应该是可行的:
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!),
//...
)https://stackoverflow.com/questions/28361530
复制相似问题