什么是这个演员的快速等价物?

时间:2014-10-30 02:26:28

标签: objective-c swift

我想把它转换成swift,或者至少找到做同样事情的东西。

 size_t width = CGImageGetWidth(spriteImage);
 size_t height = CGImageGetHeight(spriteImage);

 GLubyte * spriteData = (GLubyte *) calloc(width*height*4, sizeof(GLubyte));

我需要以正确大小的swift初始化spriteData指针。

2 个答案:

答案 0 :(得分:5)

其中前两个是size_t类型,在Swift中映射到Uint,最后一个是GLubyte,映射到UInt8。此代码会将spriteData初始化为GLubyte数组,您可以将其传递给需要UnsafeMutablePointer<GLubyte>UnsafePointer<GLubyte>的任何C函数:

let width = CGImageGetWidth(spriteImage)
let height = CGImageGetHeight(spriteImage)    
var spriteData: [GLubyte] = Array(count: Int(width * height * 4), repeatedValue: 0)

您需要对spriteData做什么?

答案 1 :(得分:1)

直截了当的方式是:

var spriteData = UnsafeMutablePointer<GLubyte>.alloc(Int(width * height * 4))

请注意,如果您这样做,则必须手动dealloc

spriteData.dealloc(Int(width * height * 4))

/// Deallocate `num` objects.
///
/// :param: num number of objects to deallocate.  Should match exactly
/// the value that was passed to `alloc()` (partial deallocations are not
/// possible).