Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory issues #19

Merged
merged 1 commit into from
Jun 9, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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