blob: 4ac4025df70f8a4ade3f446398f881425823fbdd (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
#import "UIImage+RGBAData.h"
@implementation UIImage (RGBAData)
+ (UIImage *)imageWithRGBAData:(NSData *)data width:(size_t)width height:(size_t)height
{
size_t constexpr bytesPerPixel = 4;
size_t constexpr bitsPerComponent = 8;
size_t constexpr bitsPerPixel = bitsPerComponent * bytesPerPixel;
size_t const bytesPerRow = bytesPerPixel * width;
bool constexpr shouldInterpolate = true;
CGBitmapInfo constexpr bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaLast;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data.bytes, height * bytesPerRow, NULL);
CGImageRef cgImage = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpace, bitmapInfo, provider, NULL, shouldInterpolate, kCGRenderingIntentDefault);
UIImage * image = [UIImage imageWithCGImage:cgImage];
CGColorSpaceRelease(colorSpace);
CGDataProviderRelease(provider);
CGImageRelease(cgImage);
return image;
}
@end
|