2

I've and UIImage with transparency.

I'm uploading the PNG representation of the UIImage to my web service, which accepts data which is less than let say 800K.

So basically I would like to reduce the size (NOT THE GEOMETRICAL BUT PHYSICAL I.E. in bytes) i.e. to reduce the quality of the uimage.

PLEASE DON'T POINT ME TO Quartz Guideline

PLEASE DON'T OFFER TO use JPEG representation of UIImage IT DOESNT SUPPORT TRANSPARENCY

PLEASE DON'T OFFER TO MAKE AN IMAGE SMALLER THEN TO SCALE IT

I need to reduce the colors in UIImage any ideas how to achieve it by means of Quartz ?

Following I've tried, created a context and drawn image on. Have played with bytesPerRow but still failing, please help to find the solution !

size_t width = CGImageGetWidth(image.CGImage);
size_t height = CGImageGetHeight(image.CGImage);
size_t bitsPerComponent = CGImageGetBitsPerComponent(image.CGImage);
size_t bytesPerRow = CGImageGetBytesPerRow(image.CGImage);

CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
CGContextRef ctx = CGBitmapContextCreate(NULL, // Let CG allocate it for us
                                         width,
                                         height,
                                         bitsPerComponent,
                                         bytesPerRow,
                                         colorSpace,
                                         kCGImageAlphaPremultipliedLast); // RGBA
CGColorSpaceRelease(colorSpace);
CGRect rect = CGRectMake( 0, 0, CGImageGetWidth(image.CGImage), CGImageGetHeight(image.CGImage));
CGContextDrawImage( ctx, rect, image.CGImage);

CGImageRef newImg = CGBitmapContextCreateImage(ctx);
UIImage* compressedImage = [UIImage imageWithCGImage:newImg];
CGImageRelease(newImg);

CGContextRelease(ctx);
STW
  • 44,917
  • 17
  • 105
  • 161
deimus
  • 9,565
  • 12
  • 63
  • 107
  • Sounds like you want to either compress the image and then upload it to the server, or upload it in smaller chunks that the server can handle? – Luke Jun 08 '12 at 10:47
  • Yes I want the same image with poor quality but still having transparent pixels. I dont want to split image into small chunks, but simply to reduce the quality of the image, to compress it. The same functionality like compressing images when saving in GIMP for example – deimus Jun 08 '12 at 10:52
  • I would try to separate alpha component and then apply jpeg compression. On other end decompress and apply aplha back. – RLT Aug 13 '12 at 16:21
  • I will refer you to http://optipng.sourceforge.net/ – overboming Sep 01 '12 at 04:17
  • Given that PNG is a lossless image format, which is already losslessly compressed via the DEFLATE system, and given that you're essentially trying to convert it into a lossy compression format, I'd say this is actually a pretty hard problem that's probably best solved by avoiding completely and modifying your approach to the app. Either that or try to use MacGeek's or overboming's approaches. – davidf2281 Aug 07 '13 at 16:34

1 Answers1

0

While JPEG does not support transparency, there is actually still an JPEG option.

Take the current PNG and create two files one lossy JPEG file and one PNG with only the alpha channel. ImageMagick works great for this btw.

Then load the JPEG into a UIImage and apply the PNG as a mask to the UIImage and voila you have a JPEG with transparency. Well, sort of.

I don't have my implementation here and if I did it is part of a project for a customer of mine so I would be relucted to share it, but this guide will get you started : http://jeroendeleeuw.com/post/33638733049/how-to-mask-images-with-core-graphics-in-ios and for reference my size reductions were in the neighborhood of 80%.

Best of luck.

CodeReaper
  • 5,988
  • 3
  • 35
  • 56