vilanovi / UIImage-Additions

Adding utilities to UIImage!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Improperly released graphics context causes crash

coreyfloyd opened this issue · comments

In the method below, the context was released without being the owner. Instead I used the matching UIGraphicsEndImageContext() function to return the graphics stack to the correct state.

This bug causes unpredictable crashes since there is an overreleased context floating around on the stack.

Fixed implementation below:

- (UIImage*)imageAddingImage:(UIImage*)image offset:(CGPoint)offset
{
    CGSize size = self.size;
    CGFloat scale = self.scale;

    size.width *= scale;
    size.height *= scale;

    UIGraphicsBeginImageContext(size);

    [self drawInRect:CGRectMake( 0, 0, size.width, size.height)];

    [image drawInRect:CGRectMake(scale * offset.x, scale * offset.y, image.size.width * scale, image.size.height * scale)];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGImageRef bitmapContext = CGBitmapContextCreateImage(context);
    UIImage *destImage = [UIImage imageWithCGImage:bitmapContext scale:image.scale orientation:UIImageOrientationUp];
    UIGraphicsEndImageContext();
    CGImageRelease(bitmapContext);

    return destImage;
}

Ups, thanks! Cocoapod version 1.0.3 submited!