UIImageView: Rotated and Scaled. How do I save the results?
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
(UI开发者_JS百科Image *)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;
}
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
.
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.)
精彩评论