Iphone 使用CGAffineTransform从UIImageView保存UIImage
我在UIScrollView中有一个UIImageView,使用户可以对其执行任意数量的翻转和旋转操作。我有这一切工作,允许用户缩放,平移,翻转和旋转。现在我想能够保存最后的图像出一个png 然而,这是我的头在试图解决这个问题 我见过很多其他类似的帖子,但大多数帖子只需要应用一个变换,比如旋转 我想应用用户“创建”的任何变换,这将是一系列串联在一起的翻转和旋转 当用户应用各种旋转、翻转等时,我使用CGAffineTransformConcat存储串联变换。例如,当它们旋转时,我会:Iphone 使用CGAffineTransform从UIImageView保存UIImage,iphone,objective-c,uiimage,cgcontext,Iphone,Objective C,Uiimage,Cgcontext,我在UIScrollView中有一个UIImageView,使用户可以对其执行任意数量的翻转和旋转操作。我有这一切工作,允许用户缩放,平移,翻转和旋转。现在我想能够保存最后的图像出一个png 然而,这是我的头在试图解决这个问题 我见过很多其他类似的帖子,但大多数帖子只需要应用一个变换,比如旋转 我想应用用户“创建”的任何变换,这将是一系列串联在一起的翻转和旋转 当用户应用各种旋转、翻转等时,我使用CGAffineTransformConcat存储串联变换。例如,当它们旋转时,我会: CGAffi
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;
}
偏移量是可预测的,就像总是一半的图像一样,还是取决于变换形式
struct CGAffineTransform {
CGFloat a, b, c, d;
CGFloat tx, ty;
};
如果是后者,请在使用之前在aTransform中将
tx
和ty
设置为零。偏移量是可预测的,就像总是一半图像一样,还是取决于aTransform
struct CGAffineTransform {
CGFloat a, b, c, d;
CGFloat tx, ty;
};
如果是后者,则在使用之前,请在transform中将
tx
和ty
设置为零。谢谢,但这取决于转换。最后,我添加了一个包含UIImageView的额外UIView,并使用renderContext:UIGraphicsGetCurrentContext(),然后对其进行了裁剪。谢谢,但这取决于转换。最后,我添加了一个额外的UIView来封装UIImageView,并使用renderContext:UIGraphicsGetCurrentContext(),然后对其进行裁剪。