ios - 如何从 CGColorSpace 获取 Unmanaged<CGColorSpace> ?

标签 ios swift core-graphics vimage

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

相关文章:

iOS - 以编程方式更改颜色 channel 级别

ios - Flutter ios存档应用程序 undefined symbol

ios - 使用 CoreData 和依赖注入(inject) - 线程 1 : Fatal error: init(coder:) has not been implemented

ios - 如何复制自定义对象?

iphone - 使用 CoreGraphics CGContextDrawImage 在 CATiledLayer 中绘图

ios - 使用 Swift 和 CoreGraphics 定位图像

ios - 如何分析ios ipa文件信息以减少文件大小?

swift - 无法使用 NSNib 加载 View

swift - spritekit 快速碰撞检测不起作用

ios - 是否可以在离线模式下使用 NMANavigationManager?