我在UIScrollView中有一个UIImageView,使用户能够对其执行任意数量的翻转和旋转操作。我有这一切工作,允许用户缩放,平移,翻转和旋转。现在,我希望能够将最终图像保存为png。
然而,我正在努力解决这个问题……
我见过很多类似的帖子,但大多数只需要应用一个单一的变换,如旋转,如Creating a UIImage from a rotated UIImageView
我想应用任何转换,用户已经“创建”,这将是一系列的翻转和旋转串联在一起
当用户应用各种旋转、翻转等操作时,我使用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;
}发布于 2010-06-25 12:25:35
偏移量是可预测的吗,就像总是图像的一半,还是取决于aTransform?
struct CGAffineTransform {
  CGFloat a, b, c, d;
  CGFloat tx, ty;
};如果是后者,请在使用前在aTransform中将tx和ty设置为零。
https://stackoverflow.com/questions/3115402
复制相似问题