What is the quick equivalent of this actor?

I want to convert this to quick, or at least find something that does the same.

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

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

I need to initialize the spriteData pointer in the fast correct size.

+4
source share
2 answers

The first two of them have a type size_tthat maps to Uintin Swift, and the last one GLubytethat maps to UInt8. This code initializes spriteDataas an array GLubyte, which you can pass to any C function that needs UnsafeMutablePointer<GLubyte>or UnsafePointer<GLubyte>:

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

What do you need to do with spriteData?

+5
source

A simple way is:

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

Please note that if you have done this, you must deallocmanually do it.

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).
+1
source

All Articles