2015-02-06 41 views
0

我寫在斯威夫特的功能,從一個CGImage創建vImage_CGImageFormat如下:如何從CGColorSpace獲得非託管<CGColorSpace>?

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!並且上述構造函數僅爲colorSpace參數需要Unmanaged<CGColorSpace>

是否有另一種方法可以做到這一點?也許轉換CGColorSpaceUnmanaged<CGColorSpace>

回答

2

這應該工作:

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> 

更新斯威夫特3:

vImage_CGImageFormat(
    // ... 
    colorSpace: Unmanaged.passUnretained(image.colorSpace!), 
    //... 
) 
+0

你的先生,是一個gentleme n和一位學者 – fyell 2015-02-06 08:51:42

相關問題