简体   繁体   English

UIImageView:旋转和缩放。 如何保存结果?

[英]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. 我正在尝试将旋转和缩放的UIImageView保存为UIImage的位置以及用户也编辑过的缩放比例。 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? 如何以相同的缩放和旋转方式保存UIImageView中显示的图像? I have read that I need to use UIGraphicsGetCurrentContext() but I get the same original image saved ( but flipped ) and not the rotated one!! 我已经读过我需要使用UIGraphicsGetCurrentContext(),但是我保存了相同的原始图像(但是被翻转了),而不是旋转的图像! 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;

} }

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 text core 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.) (我可能是错的。我不记得我过去做得怎么样了。把它和一粒盐一起吃。)

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 . 现在,您应该使用转换例程CGContextScaleCTMCGContextRotateCTM适当地修改您的转换矩阵,然后(避免翻转),使用-[UIImage drawAtPoint:]绘制UIImage而不是使用CGContextDrawImage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM