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

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

概述我正在尝试将此objc代码转换为 swift: CGContextRef contextRef = CGBitmapContextCreate(NULL, proposedRect.size.width, p 我正在尝试将此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)

解决方法@H_404_27@ 我最终使用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)
总结

以上是内存溢出为你收集整理的指针 – 在swift中将CFContextRef转换为可变的void指针?全部内容,希望文章能够帮你解决指针 – 在swift中将CFContextRef转换为可变的void指针?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1016693.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存