ANE,iOS和的BitmapDataANE、iOS、BitmapData

2023-09-08 11:45:27 作者:争取

有路过的位图数据转换成iOS和返回modfified副本的任何实例。我想转换一个位图到JPEG - 类似这样的例子在Windows中的 http://blog.magicalhobo.com/2011/09/12/air-3-native-extension-imagepro 处理器/但它崩溃每次。

Is there any example of passing bitmap data into iOS and returning a modfified copy. I am trying to convert a bitmap into jpeg - similar to this example in Windows http://blog.magicalhobo.com/2011/09/12/air-3-native-extension-imagepro cessor/ but it crashed every time.

我是pretty的肯定我做一些基本的错误,但没有在iOS /目标C程序员,我真的只是想魔法咒语,我无法找到任何很好的例子。任何帮助AP preciated?

I am pretty sure I am doing something basic wrong but not been an iOS / Objective C programmer I am really just trying magic incantations and I can't find any good examples. Any help appreciated?

肖恩

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

P.S. Below is my attempt:

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;

}

推荐答案

大结局的版本 - 看起来工作正常 - 恩codeS确定,我想以后自己清理确定

Finale version - seems to work ok - encodes ok and I think cleans up after itself ok.

肖恩

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;

}