我正在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/