Created
October 8, 2012 15:05
-
-
Save katopz/3852996 to your computer and use it in GitHub Desktop.
Drawing iOS UIViews to AS3 bitmapData via Adobe AIR native extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// note from http://tyleregeto.com/drawing-ios-uiviews-to-as3-bitmapdata-via-air | |
A major draw back to displaying native iOS UIView objects on top of your Adobe AIR applications is that they... well... are on top. Here is a quick code snippet for rendering native iOS view objects to ActionScript bitmapData objects via the recently released native extensions support. This allows you to add them to the display list (at the loss of all their native functionality). With native extensions we can update the bitmapData directly in memory which results in very fast updates. The possibilities are endless. | |
FREObject drawToBitmap(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]) { | |
// grab the AS3 bitmapData object for writing to | |
FREBitmapData bmd; | |
FREAcquireBitmapData(argv[0], &bmd); | |
// Draw the UIView to a UIImage object. myView is a UIView object | |
// that exists somewhere in our code. It can be any view. | |
UIGraphicsBeginImageContext(myView.bounds.size); | |
[map.layer renderInContext:UIGraphicsGetCurrentContext()]; | |
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
// Now we'll pull the raw pixels values out of the image data | |
CGImageRef imageRef = [image CGImage]; | |
NSUInteger width = CGImageGetWidth(imageRef); | |
NSUInteger height = CGImageGetHeight(imageRef); | |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | |
// Pixel color values will be written here | |
unsigned char *rawData = malloc(height * width * 4); | |
NSUInteger bytesPerPixel = 4; | |
NSUInteger bytesPerRow = bytesPerPixel * width; | |
NSUInteger bitsPerComponent = 8; | |
CGContextRef context = CGBitmapContextCreate(rawData, width, height, | |
bitsPerComponent, bytesPerRow, colorSpace, | |
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); | |
CGColorSpaceRelease(colorSpace); | |
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); | |
CGContextRelease(context); | |
// Pixels are now in rawData in the format RGBA8888 | |
// We'll now loop over each pixel write them into the AS3 bitmapData memory | |
int x, y; | |
// There may be extra pixels in each row due to the value of | |
// bmd.lineStride32, we'll skip over those as needed | |
int offset = bmd.lineStride32 - bmd.width; | |
int offset2 = bytesPerRow - bmd.width*4; | |
int byteIndex = 0; | |
uint32_t *bmdPixels = bmd.bits32; | |
// NOTE: In this example we are assuming that our AS3 bitmapData and our native UIView are the same dimensions to keep things simple. | |
for(y=0; y<bmd.height; y++) { | |
for(x=0; x<bmd.width; x++, bmdPixels ++, byteIndex += 4) { | |
// Values are currently in RGBA8888, so each colour | |
// value is currently a separate number | |
int red = (rawData[byteIndex]); | |
int green = (rawData[byteIndex + 1]); | |
int blue = (rawData[byteIndex + 2]); | |
int alpha = (rawData[byteIndex + 3]); | |
// Combine values into ARGB32 | |
* bmdPixels = (alpha << 24) | (red << 16) | (green << 8) | blue; | |
} | |
bmdPixels += offset; | |
byteIndex += offset2; | |
} | |
// free the the memory we allocated | |
free(rawData); | |
// Tell Flash which region of the bitmapData changes (all of it here) | |
FREInvalidateBitmapDataRect(argv[0], 0, 0, bmd.width, bmd.height); | |
// Release our control over the bitmapData | |
FREReleaseBitmapData(argv[0]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i guess u can set kCGBitmapByteOrder32Little instead of kCGBitmapByteOrder32Big
to get the same by order as in flash :)
then u can just simple
memcpy(as3_img_row, ios_img_row, row_bytes);
that should be faster i guess :)