1

我正在用 Swift 编写一个vImage_CGImageFormat从 a创建 a 的函数,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)returnCGColorSpace!和上面的构造函数只接受Unmanaged<CGColorSpace>参数colorSpace

还有另一种方法可以做到这一点吗?也许转换CGColorSpaceUnmanaged<CGColorSpace>?

4

1 回答 1

5

这应该有效:

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!),
    //...
)
于 2015-02-06T08:49:41.147 回答