Skip to content

Instantly share code, notes, and snippets.

@MarcoSero
Forked from krzysztofzablocki/gist:3951442
Created February 6, 2015 16:40
Show Gist options
  • Save MarcoSero/b5b9b2837d64b32d1c92 to your computer and use it in GitHub Desktop.
Save MarcoSero/b5b9b2837d64b32d1c92 to your computer and use it in GitHub Desktop.

Revisions

  1. MarcoSero renamed this gist Feb 6, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. Krzysztof Zabłocki created this gist Oct 25, 2012.
    41 changes: 41 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    + (UIImage *)decompressedImageWithImage:(UIImage *)image resizeTo:(CGSize)targetSize
    {
    CGImageRef imageRef = image.CGImage;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
    BOOL sameSize = NO;
    if (CGSizeEqualToSize(targetSize, CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)))) {
    targetSize = CGSizeMake(1, 1);
    sameSize = YES;
    }
    size_t imageWidth = (size_t)targetSize.width;
    size_t imageHeight = (size_t)targetSize.height;
    CGContextRef context = CGBitmapContextCreate(NULL,
    imageWidth,
    imageHeight,
    8,
    // Just always return width * 4 will be enough
    imageWidth * 4,
    // System only supports RGB, set explicitly
    colorSpace,
    // Makes system don't need to do extra conversion when displayed.
    alphaInfo | kCGBitmapByteOrder32Little);
    CGColorSpaceRelease(colorSpace);
    if (!context) {
    return nil;
    }


    CGRect rect = (CGRect){CGPointZero, {imageWidth, imageHeight}};
    CGContextDrawImage(context, rect, imageRef);
    if (sameSize) {
    CGContextRelease(context);
    return image;
    }
    CGImageRef decompressedImageRef = CGBitmapContextCreateImage(context);
    CGContextRelease(context);

    UIImage *decompressedImage = [[UIImage alloc] initWithCGImage:decompressedImageRef scale:image.scale orientation:image.imageOrientation];
    CGImageRelease(decompressedImageRef);
    return decompressedImage;
    }