[英]Convert CFContextRef to mutable void pointer in swift?
I'm trying to convert this objc code to swift: 我正在尝试将此objc代码转换为swift:
CGContextRef contextRef = CGBitmapContextCreate(NULL,
proposedRect.size.width,
proposedRect.size.height,
CGImageGetBitsPerComponent(imageRef),
0,
colorSpaceRef,
(CGBitmapInfo)kCGImageAlphaPremultipliedFirst);
NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithGraphicsPort:contextRef flipped:NO];
So far I ended up with this: 到目前为止,我最终得到了这个:
var bitmapContext: CGContext = CGBitmapContextCreate(data, UInt(proposedRect.width), UInt(proposedRect.height), CGImageGetBitsPerComponent(image), 0, colorSpace, CGBitmapInfo(CGImageAlphaInfo.PremultipliedFirst.toRaw()))
let context = NSGraphicsContext(graphicsPort: bitmapContext, flipped: false)
The problem is that CGBitmapContextCreate
returns CGContext
type and NSGraphicsContext
initializer accepts graphicsPort
as CMutableVoidPointer
type. 问题是
CGBitmapContextCreate
返回CGContext
类型, NSGraphicsContext
初始化程序接受graphicsPort
作为CMutableVoidPointer
类型。 So how can I convert CGContext
to CMutableVoidPointer
? 那么如何将
CGContext
转换为CMutableVoidPointer
呢?
related reverse type casting found here, but it didn't provide much help to me How to convert COpaquePointer in swift to some type (CGContext? in particular) 相关的反向类型转换在这里找到,但它没有提供太多帮助如何将swpa中的COpaquePointer转换为某种类型(特别是CGContext?)
I ended up with the reinterpretCast
function returning intermediate Int
variable for the bitmapContext
address. 我最终使用
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)
The documentation here: https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/buildingcocoaapps/InteractingWithCAPIs.html 这里的文档: https : //developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/buildingcocoaapps/InteractingWithCAPIs.html
states the following: 声明如下:
When a function is declared as taking a CMutableVoidPointer argument, it can accept the same operands as CMutablePointer for any type Type.
当函数声明为采用CMutableVoidPointer参数时,它可以为任何类型的Type接受与CMutablePointer相同的操作数。
From that, it should work like this: 从那以后,它应该像这样工作:
var p: CMutablePointer<CGContext> = &bitmapContext
NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithGraphicsPort:p flipped:NO];
Extended practically, you should be able to pass in &bitmapContext
directly to a CMutableVoidPointer
argument. 实际上,您应该能够将
&bitmapContext
直接传递给CMutableVoidPointer
参数。
Swift doesn't have "pointers" in the C sense. Swift在C意义上没有“指针”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.