如何从位图(iOS)获取原始ARGB数据?

时间:2015-09-25 03:21:35

标签: ios objective-c bitmap cgcontext rgba

机器人:

@implementation UIImage (Pixels)

-(unsigned char*) rgbaPixels
{
    // The amount of bits per pixel, in this case we are doing RGBA so 4 byte = 32 bits
    #define BITS_PER_PIXEL 32
    // The amount of bits per component, in this it is the same as the bitsPerPixel divided by 4 because each component (such as Red) is only 8 bits
    #define BITS_PER_COMPONENT (BITS_PER_PIXEL/4)
    // The amount of bytes per pixel, in this case a pixel is made up of Red, Green, Blue and Alpha so it will be 4
    #define BYTES_PER_PIXEL (BITS_PER_PIXEL/BITS_PER_COMPONENT)

    // Define the colour space (in this case it's gray)
    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();

    // Find out the number of bytes per row (it's just the width times the number of bytes per pixel)
    size_t bytesPerRow = self.size.width * BYTES_PER_PIXEL;
    // Allocate the appropriate amount of memory to hold the bitmap context
    unsigned char* bitmapData = (unsigned char*) malloc(bytesPerRow*self.size.height);

    // Create the bitmap context, we set the alpha to none here to tell the bitmap we don't care about alpha values
    CGContextRef context = CGBitmapContextCreate(bitmapData,self.size.width,self.size.height,BITS_PER_COMPONENT,bytesPerRow,colourSpace,kCGImageAlphaFirst);//It returns null

    /* We are done with the colour space now so no point in keeping it around*/
    CGColorSpaceRelease(colourSpace);

    // Create a CGRect to define the amount of pixels we want
    CGRect rect = CGRectMake(0.0,0.0,self.size.width,self.size.height);
    // Draw the bitmap context using the rectangle we just created as a bounds and the Core Graphics Image as the image source
    CGContextDrawImage(context,rect,self.CGImage);
    // Obtain the pixel data from the bitmap context
    unsigned char* pixelData = (unsigned char*)CGBitmapContextGetData(context);

    // Release the bitmap context because we are done using it
    CGContextRelease(context);
    //CGColorSpaceRelease(colourSpace);

    return pixelData;
    #undef BITS_PER_PIXEL
    #undef BITS_PER_COMPONENT
}

以像素[]的形式返回位图中数据的副本。   每个值都是一个表示Color的压缩int。   stride参数允许调用者允许行之间返回的像素数组中的间隙。   对于正常打包结果,只需传递步幅值的宽度。   返回的颜色是非预乘的ARGB值。

的iOS:

CGBitmapContextCreate(bitmapData,self.size.width,self.size.height,BITS_PER_COMPONENT,bytesPerRow,colourSpace,kCGImageAlphaFirst);

但它不起作用。

String s3= new String("A");

返回NULL。

我需要与上面的 pix [] 相同的数组,我该如何制作呢?

0 个答案:

没有答案