你如何从 CGColorSpace 获得 Unmanaged<CGColorSpace>?
How do you get Unmanaged<CGColorSpace> from CGColorSpace?
我正在 Swift 中编写一个函数,它从 CGImage
创建一个 vImage_CGImageFormat
,如下所示:
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)
returns 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>
Swift3 的更新:
vImage_CGImageFormat(
// ...
colorSpace: Unmanaged.passUnretained(image.colorSpace!),
//...
)
我正在 Swift 中编写一个函数,它从 CGImage
创建一个 vImage_CGImageFormat
,如下所示:
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)
returns 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>
Swift3 的更新:
vImage_CGImageFormat(
// ...
colorSpace: Unmanaged.passUnretained(image.colorSpace!),
//...
)