gpt4 book ai didi

ios - 你如何从 CGColorSpace 获得 Unmanaged

转载 作者:搜寻专家 更新时间:2023-11-01 06:23:45 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com