views:

363

answers:

2

Hi All, I am trying to save a rotated and scaled UIImageView as an UIImage in the position and with the zoom scale that the user edited too. But I can only manage to save the original image as it was before editing. How can I save the image as it is shown in the UIImageView with the same scaling and rotation? I have read that I need to use UIGraphicsGetCurrentContext() but I get the same original image saved ( but flipped ) and not the rotated one!! Suggestions and hints would be really helpfull. Thank You in advance. Al

(UIImage *)getImage
{
CGSize size = CGSizeMake(250, 250);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGMutablePathRef path = CGPathCreateMutable();

// Add circle to path
CGPathAddEllipseInRect(path, NULL, CGRectMake(0, 0, 250, 250));
CGContextAddPath(context, path);

// ****************** touchImageView is my UIImageView ****************//
CGContextDrawImage(context, CGRectMake(0, 0, 250, 250), [touchImageView image].CGImage);

UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();

// Clip to the circle and draw the logo
CGContextClip(context);
UIGraphicsEndImageContext();

return scaledImage;

}

A: 

The view based transforms are applied the output of the context not the context itself. I believe you have to scale and rotate the image context itself using the link textcore graphic's specific functions.

(I could be wrong. I can't remember how exactly how I did this in the past.Take this with a grain of salt.)

TechZen
A: 

I don't see anything in the code you posted that would rotate an image. Did you forget to add that?

It looks like you are on the right track. Now you should use the transform routines CGContextScaleCTM and CGContextRotateCTM to modify your transform matrix appropriately, and then (to avoid the flipping), use -[UIImage drawAtPoint:] to draw the UIImage instead of using CGContextDrawImage.

Ken Aspeslagh
The rotation takes place in the touchImageView and then I want to save the rorated image as a circle. Sorry if I was unclear.
Alan Aherne
You won't be able to do it like that. The UIImage itself won't get rotated when you rotate the UIImageView. You'll have to rotate it yourself before drawing it if you want to save it. See my answer above for how to do this.
Ken Aspeslagh