views:

694

answers:

2

I basically want to automatically create a tiled image from a bunch of source images, and then save that to the user's photo album. I'm not having any success drawing a bunch of small UIImage's into one big UIImage. What's the best way to accomplish this? Currently I'm using UIGraphicsBeginImageContext() and [UIImage drawAtPoint], etc. All I ever end up with is a 512x512 black square. How should I be doing this? I'm looking to CGLayer's, etc. seems there are a lot of options but none that work particularly easily.

Let me actually put my code in:

CGSize size = CGSizeMake(512, 512);
UIGraphicsBeginImageContext(size);
UIGraphicsPushContext(UIGraphicsGetCurrentContext());
for (int i = 0; i < 4; i++)
{
    for (int j = 0; j < 4; j++)
    {
        UIImage *image = [self getImageAt:i:j];
        [image drawAtPoint:CGPointMake(i*128,j*128)];
    }
}
UIGraphicsPopContext();
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(result, nil, nil, nil);

I should note that actually the above is not what happens in my code exactly. What really happens is that I call every line before and including to UIGraphicsPushContext, then in an animation timer I slowly increment the drawing and draw to the context. Then after it's all done I call everything from UIGraphicsPopContext onward.

+1  A: 

Oh, then you can just save the onscreen view after it has been rendered on screen:

 UIGraphicsBeginImageContext(myBigView.bounds.size);
    [view drawRect:myBigView.bounds];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

are you storing it back to an image?

UIImage *myBigImage = UIGraphicsGetImageFromCurrentImageContext();
Corey Floyd
Yes, I put in code now so you can see exactly what I did.
Eli
Ok, try getting rid of the lines: UIGraphicsPushContext(UIGraphicsGetCurrentContext());and UIGraphicsPopContext();
Corey Floyd
Nope, I've got an OpenGL view which is the current graphics context and no other UIView anywhere in existence. I am screenshotting OpenGL data and trying to tile it into a completely different UIImage. The purpose of this is basically to get 3D models from multiple angles in one sprite sheet.
Eli
Something doesn't feel right about your context (This is mostly hunch stuff, as I don't play with OpenGL). I feel like that is why you are getting a black square because your not drawing in the context that you think you are. Can you do this: UIGraphicsPushContext(UIGraphicsBeginImageContext(size)); or does that throw an exception?
Corey Floyd
A: 

To do exactly what I wanted to do...

Make your GLView as big as the total thing you want. Also make sure glOrtho and your viewport have the right size. Then just draw whatever you want wherever you want, and take a single OpenGL screenshot. Then you don't need to worry about combining to a single UIImage over multiple OpenGL rendering passes, which is no doubt what was causing my issue.

Eli