UIImage 取 RGBA , RBGA封装 UIImage

2017-08-15  本文已影响135人  rogerwu1228

UIImage 2 RGBA data

- (unsigned char *)getImagePixel
{
    UIImage *image = [UIImage imageNamed:@"test_test.jpg"];
    CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
    const uint8_t* data = CFDataGetBytePtr(pixelData);
    
    int width = (int)image.size.width;
    int height = (int)image.size.height;
    
    size_t bitsPerPixel = CGImageGetBitsPerPixel(image.CGImage);
    printf("bitsPerPixel = %lu\n", bitsPerPixel);
    size_t gitsPerComponent = CGImageGetBitsPerComponent(image.CGImage);
    printf("gitsPerComponent = %lu\n", gitsPerComponent);
    
    uint8_t *imgData = (uint8_t *)malloc(width*height*4);
    memcpy(imgData, data, width*height*4);
    return imgData;
}

RGBA 2 UIImage

void convertBitsDataIntoUIImage( uint32_t size , uint32_t width , uint32_t height , void*bitsData )
{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();//
    void *colorData = bitsData;
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, colorData, width * height * 4, NULL);
    
    CGImageRef cgImage2 = CGImageCreate(width,  //w
                                        height,  //h
                                        8,    //bitsPerComponent
                                        8 * 4,  //bitsPerPixel
                                        width*4,  //bytesPerRow
                                        colorSpace,
                                        kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault,
                                        provider,
                                        NULL,
                                        NO,
                                        kCGRenderingIntentDefault);
    UIImage *image = [UIImage imageWithCGImage:cgImage2];
    NSLog(@"-------%@",image);
    
    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(cgImage2);
}
上一篇下一篇

猜你喜欢

热点阅读