首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用CGAffineTransform从UIImageView保存UIImage

使用CGAffineTransform从UIImageView保存UIImage
EN

Stack Overflow用户
提问于 2010-06-25 11:41:25
回答 1查看 4.2K关注 0票数 1

我在UIScrollView中有一个UIImageView,使用户能够对其执行任意数量的翻转和旋转操作。我有这一切工作,允许用户缩放,平移,翻转和旋转。现在,我希望能够将最终图像保存为png。

然而,我正在努力解决这个问题……

我见过很多类似的帖子,但大多数只需要应用一个单一的变换,如旋转,如Creating a UIImage from a rotated UIImageView

我想应用任何转换,用户已经“创建”,这将是一系列的翻转和旋转串联在一起

当用户应用各种旋转、翻转等操作时,我使用CGAffineTransformConcat存储连接后的变换。例如,当它们旋转时,我会这样做:

代码语言:javascript
运行
复制
CGAffineTransform newTransform = CGAffineTransformMakeRotation(angle);
self.theFullTransform = CGAffineTransformConcat(self.theFullTransform, newTransform);
self.fullPhotoImageView.transform = self.theFullTransform;

下面的方法是我迄今为止获得的最好的方法,用来创建一个带有完整转换的UIImage,但是图像总是被翻译到错误的位置。例如,图像是“偏移”的。我猜测这可能与使用CGAffineTransformTranslate或CGContextDrawImage中设置的错误边界有关。

有谁有什么想法吗?这看起来比我想的要难得多。

代码语言:javascript
运行
复制
- (UIImage *) translateImageFromImageView: (UIImageView *) imageView withTransform:(CGAffineTransform) aTransform
{
    UIImage *rotatedImage;

    // Get image width, height of the bounding rectangle
    CGRect boundingRect = CGRectApplyAffineTransform(imageView.bounds, aTransform);

    // Create a graphics context the size of the bounding rectangle
    UIGraphicsBeginImageContext(boundingRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGAffineTransform transform = CGAffineTransformIdentity;

    //I think this translaton is the problem?

    transform = CGAffineTransformTranslate(transform, boundingRect.size.width/2, boundingRect.size.height/2);
    transform = CGAffineTransformScale(transform, 1.0, -1.0);
    transform = CGAffineTransformConcat(transform, aTransform);

    CGContextConcatCTM(context, transform);

    // Draw the image into the context

    // or the boundingRect is incorrect here?

    CGContextDrawImage(context, boundingRect, imageView.image.CGImage);

    // Get an image from the context
    rotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)];

    // Clean up
    UIGraphicsEndImageContext();
    return rotatedImage;
}
EN

Stack Overflow用户

发布于 2010-06-25 12:25:35

偏移量是可预测的吗,就像总是图像的一半,还是取决于aTransform?

代码语言:javascript
运行
复制
struct CGAffineTransform {
  CGFloat a, b, c, d;
  CGFloat tx, ty;
};

如果是后者,请在使用前在aTransform中将txty设置为零。

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3115402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档