RGBA Data 转 UIImage

    size_t  width = 90;

    size_t  height = 90;

    size_t bitsPerComponent = 8; // 32/4 (4: R G BA)

    size_t bytesPerRow = 4 * width;

    

    unsigned char *imageBytes = malloc(bytesPerRow * height);

    memset(imageBytes, 0, bytesPerRow * height);

    [data getBytes:imageBytes length:data.length];

    

    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();

    

    CGContextRef contextRef = CGBitmapContextCreate(imageBytes,

                                                    width,

                                                    height,

                                                    bitsPerComponent,

                                                    bytesPerRow,

                                                    colorSpaceRef,

                                                    kCGImageAlphaPremultipliedLast);

    

    CGImageRef imageRef = CGBitmapContextCreateImage(contextRef);

    UIImage *image = [UIImage imageWithCGImage:imageRef];

    CGContextRelease(contextRef);

    CGColorSpaceRelease(colorSpaceRef);

    free(imageBytes);

猜你喜欢

转载自blog.csdn.net/majiakun1/article/details/84646910