Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone SDK - How to draw a UIImage onto another UIImage?

I have several rectangular images (in landscape and portrait mode) and want to draw them onto a transparent square image, so that all images became the same size without cropping them. How would I create a transparent UIImage and draw another on top?

Thanks for any hints.

like image 442
dan Avatar asked Feb 27 '10 21:02

dan


People also ask

How do I copy UIImage?

Deep copying UIImage *newImage = [UIImage imageWithData:UIImagePNGRepresentation(oldImage)]; This will copy the data but will require setting the orientation property before handing it to something like UIImageView for proper display. Another way to deep copy would be to draw into the context and grab the result.

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .


1 Answers

  1. Create a bitmap graphic context with CGBitmapContextCreate. You'll need to determine what the size of the resulting composite image here. You can think of this as a sort of canvas.
  2. Draw the images using CGContextDrawImage. This will draw images onto the same context.
  3. Once you're done drawing all the images into the same context, create an image from that context with CGBitmapContextCreateImage.
  4. Convert the Core Graphics image from step #3 into a UIImage with [UIImage imageWithCGIImage:].

Code examples can be found here.

like image 109
Giao Avatar answered Sep 23 '22 08:09

Giao