ANE,iOS和BitmapData

时间:2011-12-15 17:45:48

标签: ios actionscript air air-native-extension

是否有任何将位图数据传递到iOS并返回修改后的副本的示例。我试图将位图转换为jpeg - 类似于Windows http://blog.magicalhobo.com/2011/09/12/air-3-native-extension-imagepro cessor /中的此示例,但每次都崩溃了。

我很确定我做了一些基本的错误但不是iOS / Objective C程序员我真的只是在尝试魔术咒语,我找不到任何好的例子。有什么帮助表示赞赏吗?

肖恩

P.S。以下是我的尝试:

FREObject encodeJPEG(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[])
{
  FREObject     objectBitmapData = argv[0];
  FREBitmapData bitmapData;

  FREAcquireBitmapData(objectBitmapData, &bitmapData);

  int width       = bitmapData.width;
  int height      = bitmapData.height;
  int stride      = bitmapData.lineStride32 * 4;
  uint32_t* input = bitmapData.bits32;

  FREReleaseBitmapData(objectBitmapData);


  UIImage *myImage = [UIImage imageWithData:input];
  NSData *jpgData  = UIImageJPEGRepresentation(myImage, 0.9);    

  FREObject    objectByteArray = argv[1];
  FREByteArray byteArray;

  FREObject length;

  FRENewObjectFromUint32(jpgData.length, &length);

  FRESetObjectProperty(objectByteArray, (const uint8_t*) "length", length, NULL);

  FREAcquireByteArray(objectByteArray, &byteArray);

  memcpy(byteArray.bytes, jpgData.bytes, jpgData.length);
  FREReleaseByteArray(objectByteArray);

  return NULL;

}

3 个答案:

答案 0 :(得分:3)

结局版本 - 似乎工作正常 - 编码正常,我认为自己确定后可以清理。

肖恩

FREObject encodeJPEG( FREContext ctx, void* funcData, uint32_t argc, FREObject argv[] )
{
FREObject       objectBitmapData = argv[ 0 ];
FREBitmapData2  bitmapData;

FREAcquireBitmapData2( objectBitmapData, &bitmapData );

int width       = bitmapData.width;
int height      = bitmapData.height;
int stride      = bitmapData.lineStride32 * 4;
uint32_t* input = bitmapData.bits32;

// make data provider from buffer
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, bitmapData.bits32, (width * height * 4), NULL);

// set up for CGImage creation
int                     bitsPerComponent    = 8;
int                     bitsPerPixel        = 32;
int                     bytesPerRow         = 4 * width;
CGColorSpaceRef         colorSpaceRef       = CGColorSpaceCreateDeviceRGB();    
CGBitmapInfo            bitmapInfo;

if( bitmapData.hasAlpha )
{
    if( bitmapData.isPremultiplied )
        bitmapInfo = kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst; 
    else
        bitmapInfo = kCGBitmapByteOrder32Little | kCGImageAlphaFirst;            
}
else
{
    bitmapInfo = kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst;  
}

CGColorRenderingIntent  renderingIntent     = kCGRenderingIntentDefault;
CGImageRef              imageRef            = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

// make UIImage from CGImage
UIImage *myImage = [UIImage imageWithCGImage:imageRef];    
NSData* jpgData  = UIImageJPEGRepresentation( myImage, 0.9 );    

FREReleaseBitmapData( objectBitmapData );

FREObject    objectByteArray = argv[ 1 ];               
FREByteArray byteArray;
FREObject    length;

FRENewObjectFromUint32( jpgData.length, &length );
FRESetObjectProperty( objectByteArray, ( const uint8_t* ) "length", length, NULL );
FREAcquireByteArray( objectByteArray, &byteArray );

memcpy( byteArray.bytes, jpgData.bytes, jpgData.length );

FREReleaseByteArray( objectByteArray );

// release bits
CGColorSpaceRelease(colorSpaceRef);
CGImageRelease(imageRef);
CGDataProviderRelease(provider);    
return NULL;

}

答案 1 :(得分:0)

除了红色是jpeg中的蓝调之外,这种方式有效,并且不确定我是否正在释放所有内容。

那是明天的战斗。

肖恩

FREObject encodeJPEG( FREContext ctx, void* funcData, uint32_t argc, FREObject argv[] )  
{
 FREObject       objectBitmapData = argv[ 0 ];
 FREBitmapData2  bitmapData;

 FREAcquireBitmapData2( objectBitmapData, &bitmapData );

 int width       = bitmapData.width;
 int height      = bitmapData.height;
 int stride      = bitmapData.lineStride32 * 4;
 uint32_t* input = bitmapData.bits32;

 // make data provider from buffer
 CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, bitmapData.bits32, (width * height * 4), NULL);

 // set up for CGImage creation
 int                     bitsPerComponent    = 8;
 int                     bitsPerPixel        = 32;
 int                     bytesPerRow         = 4 * width;
 CGColorSpaceRef         colorSpaceRef       = CGColorSpaceCreateDeviceRGB();
 CGBitmapInfo            bitmapInfo          = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst;
 CGColorRenderingIntent  renderingIntent     = kCGRenderingIntentDefault;
 CGImageRef              imageRef            = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

 // make UIImage from CGImage
 UIImage *myImage = [UIImage imageWithCGImage:imageRef];    
 NSData* jpgData  = UIImageJPEGRepresentation( myImage, 0.9 );    

 FREReleaseBitmapData( objectBitmapData );

 FREObject    objectByteArray = argv[ 1 ];               
 FREByteArray byteArray;
 FREObject    length;

 FRENewObjectFromUint32( jpgData.length, &length );
 FRESetObjectProperty( objectByteArray, ( const uint8_t* ) "length", length, NULL );
 FREAcquireByteArray( objectByteArray, &byteArray );

 memcpy( byteArray.bytes, jpgData.bytes, jpgData.length );

 FREReleaseByteArray( objectByteArray );

 return NULL;
}

答案 2 :(得分:0)

交换红色和蓝色像素的位置

您从iOS获得的格式为:0xAARRGGBB
预期在AIR应用程序上显示的那个是:0xAABBGGRR