加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

指针 – 在swift中将CFContextRef转换为可变的void指针?

发布时间:2020-12-14 04:57:19 所属栏目:百科 来源:网络整理
导读:我正在尝试将此objc代码转换为 swift: CGContextRef contextRef = CGBitmapContextCreate(NULL,proposedRect.size.width,proposedRect.size.height,CGImageGetBitsPerComponent(imageRef),colorSpaceRef,(CGBitmapInfo)kCGImageAlphaPremultipliedFirst);NS
我正在尝试将此objc代码转换为 swift:

CGContextRef contextRef = CGBitmapContextCreate(NULL,proposedRect.size.width,proposedRect.size.height,CGImageGetBitsPerComponent(imageRef),colorSpaceRef,(CGBitmapInfo)kCGImageAlphaPremultipliedFirst);
NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithGraphicsPort:contextRef flipped:NO];

到目前为止,我最终得到了这个:

var bitmapContext: CGContext = CGBitmapContextCreate(data,UInt(proposedRect.width),UInt(proposedRect.height),CGImageGetBitsPerComponent(image),colorSpace,CGBitmapInfo(CGImageAlphaInfo.PremultipliedFirst.toRaw()))
let context = NSGraphicsContext(graphicsPort: bitmapContext,flipped: false)

问题是CGBitmapContextCreate返回CGContext类型,NSGraphicsContext初始化程序接受graphicsPort作为CMutableVoidPointer类型.那么如何将CGContext转换为CMutableVoidPointer呢?

相关的反向型铸造在这里找到,但它没有给我提供太多帮助
How to convert COpaquePointer in swift to some type (CGContext? in particular)

解决方法

我最终使用reinterpretCast函数返回bitmapContext地址的中间Int变量.

var bitmapContext: CGContext = CGBitmapContextCreate(...)

let bitmapContextAddress: Int = reinterpretCast(bitmapContext)
let bitmapContextPointer: CMutableVoidPointer = COpaquePointer(UnsafePointer<CGContext>(bitmapContextAddress))
let context = NSGraphicsContext(graphicsPort: bitmapContextPointer,flipped: false)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读