Skip to content

Commit

Permalink
Merge pull request #19 from brandons/master
Browse files Browse the repository at this point in the history
Fix memory issues
  • Loading branch information
seanooi committed Jun 9, 2015
2 parents 97e7a14 + a83768a commit c8c7fb2
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions iOS-WebP/UIImage+WebP.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
// This gets called when the UIImage gets collected and frees the underlying image.
static void free_image_data(void *info, const void *data, size_t size)
{
if(info != NULL)
WebPFreeDecBuffer(&(((WebPDecoderConfig *)info)->output));
else
free((void *)data);
if(info != NULL) {
WebPFreeDecBuffer(&(((WebPDecoderConfig *) info)->output));
free(info);
} else {
free((void *) data);
}
}

@implementation UIImage (WebP)
Expand Down Expand Up @@ -124,22 +126,22 @@ + (UIImage *)imageWithWebPData:(NSData *)imgData error:(NSError **)error
return nil;
}

WebPDecoderConfig config;
if(!WebPInitDecoderConfig(&config)) {
WebPDecoderConfig * config = malloc(sizeof(WebPDecoderConfig));
if(!WebPInitDecoderConfig(config)) {
NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary];
[errorDetail setValue:@"Failed to initialize structure. Version mismatch." forKey:NSLocalizedDescriptionKey];
if(error != NULL)
*error = [NSError errorWithDomain:[NSString stringWithFormat:@"%@.errorDomain", [[NSBundle mainBundle] bundleIdentifier]] code:-101 userInfo:errorDetail];
return nil;
}

config.options.no_fancy_upsampling = 1;
config.options.bypass_filtering = 1;
config.options.use_threads = 1;
config.output.colorspace = MODE_RGBA;
config->options.no_fancy_upsampling = 1;
config->options.bypass_filtering = 1;
config->options.use_threads = 1;
config->output.colorspace = MODE_RGBA;

// Decode the WebP image data into a RGBA value array
VP8StatusCode decodeStatus = WebPDecode([imgData bytes], [imgData length], &config);
VP8StatusCode decodeStatus = WebPDecode([imgData bytes], [imgData length], config);
if (decodeStatus != VP8_STATUS_OK) {
NSString *errorString = [self statusForVP8Code:decodeStatus];

Expand All @@ -152,7 +154,7 @@ + (UIImage *)imageWithWebPData:(NSData *)imgData error:(NSError **)error

// Construct UIImage from the decoded RGBA value array
uint8_t *data = WebPDecodeRGBA([imgData bytes], [imgData length], &width, &height);
CGDataProviderRef provider = CGDataProviderCreateWithData(&config, data, config.options.scaled_width * config.options.scaled_height * 4, free_image_data);
CGDataProviderRef provider = CGDataProviderCreateWithData(config, data, config->options.scaled_width * config->options.scaled_height * 4, free_image_data);

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault |kCGImageAlphaLast;
Expand All @@ -165,7 +167,6 @@ + (UIImage *)imageWithWebPData:(NSData *)imgData error:(NSError **)error
CGImageRelease(imageRef);
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);
WebPFreeDecBuffer(&config.output);

return result;
}
Expand Down

0 comments on commit c8c7fb2

Please sign in to comment.