I have a problem with a simple iPad image collage app. I have a UIImageView where it’s possible to drag’n’drop different UIImageViews into. The dragged views can be rotated and stretched. When the user have finished editing the image, I’ll merge all the images in on final image. My solution have been to use UIGraphics and draw the large image, then loop through all subviews and draw them in the context:
- (UIImage*)mergeImages
{
CGSize imageSize = imageView.frame.size;
UIGraphicsBeginImageContext(imageSize);
[imageView.image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
[[imageView subviews] enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
UIImage *imageOriginal = ((UIImageView*)object).image;
CGRect rect = ((UIImageView*)object).frame;
UIImage *imageToAdd = [self scaleImage:imageOriginal :rect.size];
// Find the w/h ratioes
float widthRatio = rect.size.width / imageOriginal.size.width;
float heightRatio = rect.size.height / imageOriginal.size.height;
// Calculate point to draw at
CGPoint pointToDrawAt;
if (widthRatio > heightRatio)
pointToDrawAt = CGPointMake(rect.origin.x + (rect.size.width / 2) - (imageToAdd.size.width / 2), rect.origin.y);
else
pointToDrawAt = CGPointMake(rect.origin.x, rect.origin.y + (rect.size.width / 2) - (imageToAdd.size.height / 2));
[imageToAdd drawAtPoint:pointToDrawAt];
}];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Save image in photogalery
UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);
return newImage;
}
The above works fines as long the images isn’t rotated. So my question is:
Is there a way I can find the rotation angle from the subview, or do I have to subclass UIView and make it a property? Or is there another way I can merge images and get the drawing specific properties like angle and filters?
If I can help with further code or anything just comment. And thanks in advance 🙂
UIImages have a property called “imageOrientation” which can be queried. Once you know this, you can apply a CGAffineTransform to the context to get the image to render as you see it. There was a recent posting on this – in the past few days – if you search on ‘CGAffineTransform’ you should be able to find it (or a similar post) and see how its done.