Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

KardelSharpeye

macrumors member
Original poster
Apr 28, 2009
56
0
Good morning friends,

I was just wondering if there is anyway to draw an image with its transform values into CGContext. basically i have the following code.

Code:
// Size of the new image.
CGSize size = CGSizeMake(width, height);

// Context.
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();

// Your images here.
UIImage *image1;
UIImage *image2;

// Draw them to the context.
[image1 drawInRect:CGRectMake(x, y, width, height)];
[image2 drawInRect:CGRectMake(x, y, width, height)];

// Construct the image from the context and return it.
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage; // The thing you want.

but after looking at the image all the previous rotation is gone. scaling however works which is kinda strange.

any help is greatly appreciated.
 
I don't see too much rotation going on. I see you getting the current graphics context and then immediately drawing the images.

Where are you making the rotations happen?
 
I don't see too much rotation going on. I see you getting the current graphics context and then immediately drawing the images.

Where are you making the rotations happen?

sorry this is my save image only.

Code:
// Size of the new image.
CGSize size = CGSizeMake(width, height);

// Context.
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();

// Your images here.
UIImage *image1 = imageSelected1.image;
UIImage *image2 = imageSelected2.image;

// Draw them to the context.
[image1 drawInRect:CGRectMake(x, y, width, height)];
[image2 drawInRect:CGRectMake(x, y, width, height)];

// Construct the image from the context and return it.
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage; // The thing you want.

and i have:
Code:
UIImageView imageSelected1, imageSelected2;

imageSelected1.transform = CGAffineTransformRotate( imageSelected1.transform, degreesToRadians(3));

imageSelected2.transform = CGAffineTransformRotate( imageSelected2.transform, degreesToRadians(3));
in main which is called before that save function
 
I don't have too much experience with that transform property; I would just do something like:

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextRotateCTM(context, angle);

[image1 drawInRect:CGRectMake(x, y, width, height)];
[image2 drawInRect:CGRectMake(x, y, width, height)];

Which should work. Now that I started working with Core Graphics more, I found myself dropping down instead of using the UIKit additions. If you can find a way to make the rotation work without the CGContextRotateCTM function call, that might be ideal.
 
I don't have too much experience with that transform property; I would just do something like:

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextRotateCTM(context, angle);

[image1 drawInRect:CGRectMake(x, y, width, height)];
[image2 drawInRect:CGRectMake(x, y, width, height)];

Which should work. Now that I started working with Core Graphics more, I found myself dropping down instead of using the UIKit additions. If you can find a way to make the rotation work without the CGContextRotateCTM function call, that might be ideal.

omg thank you so much that worked!!! yeah there is so much for me to learn right now because i only have been into this for less than a month. but yes thank you again!
 
Now that I think about it, you were originally applying a transformation to the *view* -- this shouldn't have affected the *images*, which is why you were not seeing the rotations.
 
Now that I think about it, you were originally applying a transformation to the *view* -- this shouldn't have affected the *images*, which is why you were not seeing the rotations.

yeh but UIImageView is the only one that has transform property how else can i rotate an image? also now when i save it my coordinates is a bit off. any idea why this is happenning? i was thinkng bout using convertPoint:toView but it doesnt make sense because i am actually using the image coords...in the currentview(self)...
 
What happens if, instead of transforming the context and then drawing, you draw and *then* transform the context? Does that make any difference?
 
What happens if, instead of transforming the context and then drawing, you draw and *then* transform the context? Does that make any difference?

image will not rotate if i transform the context AFTER i drawInRect...also it seems that the more angle i apply to the context the further the image move away from the source when is it drawn in the context. and when i move the angle back to its original angle or a full 360degrees. the image saves like it normoally would....
 
Like I said, I'm don't use the UIKit drawing functions, too much, but my guess is that you might want to try re-evaluating the size parameter that you are passing to the bitmap graphics context. You might want to make the size large enough to contain the whole rotated image, if you know what I mean. I'm not sure if the UIKit function will crop or resize if the size is too small. This might help you position the origin better.

You can also apply another transform to the rotated image to bring the origin of the image back where you want it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.