我在 UIScrollView 中有一个 UIImageView,我使用户能够对其执行任意数量的翻转和旋转操作。我有这一切工作,允许用户缩放、平移、翻转和旋转。现在我希望能够将最终图像保存为 png。
然而,它正在努力解决这个问题......
我已经看到了很多与此类似的其他帖子,但大多数只需要应用单个变换,例如旋转,例如从旋转的 UIImageView 创建 UIImage
我想应用用户“创建”的任何变换,这将是一系列连接在一起的翻转和旋转
当用户应用各种旋转、翻转等时,我使用 CGAffineTransformConcat 存储级联变换。例如,当他们旋转时,我会:
CGAffineTransform newTransform = CGAffineTransformMakeRotation(angle);
self.theFullTransform = CGAffineTransformConcat(self.theFullTransform, newTransform);
self.fullPhotoImageView.transform = self.theFullTransform;
以下方法是迄今为止我用完整变换创建 UIImage 的最佳方法,但是图像总是被翻译在错误的位置。例如,图像是“偏移”。我的猜测与使用在 CGAffineTransformTranslate 或 CGContextDrawImage 中设置的错误边界有关。
有没有人有任何想法?这似乎比我认为的要难得多......
- (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;
}