Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'Flatten' multiple UIImageViews into ONE?

I have a feeling this is not an easy task but I need to combine or flatten a UIImageView with another UIImage view lying above it. For example: I have two UIImageViews. One of them has a UIImage of a grassy field (1200 x 1200 pixels). The other is a UIImage of a basketball (128 x 128 pixels), and it is positioned above the image of the grassy field in such a way that the basketball appears to be on the grassy field. I want to be able to SAVE the superimposed UIImageViews as a single image file to my photo album which means that I will need to combine the two images somehow. How would this be accomplished? (NOTE: Taking a screenshot (320 x 480 pixels) would not be an acceptable solution as I wish to preserve the size of 1200 x 1600 pixels.

QUESTION:
How can I flatten multiple UIImageViews into one and SAVE the resulting image while preserving the size/resolution.

like image 206
RexOnRoids Avatar asked Aug 10 '09 06:08

RexOnRoids


1 Answers

Why don't you just draw original UIImages into a background buffer on top of each other and then write a combined image to a file? Below is an example how you can draw two images to the same buffer:

CGImageRef bgimage = [bguiimage CGImage];
width = CGImageGetWidth(bgimage);
height = CGImageGetHeight(bgimage);

// Create a temporary texture data buffer
GLUbyte* data = (GLubyte *) malloc(width * height * 4);
assert(data);

// Draw image to buffer
CGContextRef ctx = CGBitmapContextCreate(data, width, height, 8, width * 4, CGImageGetColorSpace(image), kCGImageAlphaPremultipliedLast);
assert(ctx);

// Flip image upside-down because OpenGL coordinates differ
CGContextTranslateCTM(ctx, 0, height);
CGContextScaleCTM(ctx, 1.0, -1.0);

CGContextDrawImage(ctx, CGRectMake(0, 0, (CGFloat)width, (CGFloat)height), bgimage);

CGImageRef ballimage = [balluiimage CGImage];
bwidth = CGImageGetWidth(ballimage);
bheight = CGImageGetHeight(ballimage);

float x = (width - bwidth) / 2.0;
float y = (height - bheight) / 2.0;
CGContextDrawImage(ctx, CGRectMake(x, y, (CGFloat)bwidth, (CGFloat)bheight), ballimage);

CGContextRelease(ctx);
like image 136
Teemu Kurppa Avatar answered Oct 22 '22 21:10

Teemu Kurppa