我正在尝试将此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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。