forked from dbasedow/react-native-webp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBAWebpImageDecoder.m
More file actions
53 lines (42 loc) · 1.77 KB
/
DBAWebpImageDecoder.m
File metadata and controls
53 lines (42 loc) · 1.77 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#import "DBAWebpImageDecoder.h"
#include "WebP/decode.h"
#include "WebP/demux.h"
static void free_data(void *info, const void *data, size_t size)
{
free((void *) data);
}
@implementation DBAWebpImageDecoder
RCT_EXPORT_MODULE()
- (BOOL)canDecodeImageData:(NSData *)imageData
{
int result = WebPGetInfo([imageData bytes], [imageData length], NULL, NULL);
if (result == 0) {
return NO;
} else {
return YES;
}
}
- (RCTImageLoaderCancellationBlock)decodeImageData:(NSData *)imageData
size:(CGSize)size
scale:(CGFloat)scale
resizeMode:(UIViewContentMode)resizeMode
completionHandler:(RCTImageLoaderCompletionBlock)completionHandler
{
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaLast;
UIImage *image;
WebPBitstreamFeatures features;
WebPGetFeatures([imageData bytes], [imageData length], &features);
int width = 0, height = 0;
uint8_t *data = WebPDecodeRGBA([imageData bytes], [imageData length], &width, &height);
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data, width*height*4, free_data);
CGImageRef imageRef = CGImageCreate(width, height, 8, 32, 4 * width, colorSpaceRef, bitmapInfo, provider, NULL, YES, renderingIntent);
image = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp];
CGDataProviderRelease(provider);
CGImageRelease(imageRef);
CGColorSpaceRelease(colorSpaceRef);
completionHandler(nil, image);
return ^{};
}
@end