我想保存由用户移动,调整大小和旋转的2个UIImages。问题是我不想使用任何'printscreen one'这样的功能,因为它会使两个图像的质量(分辨率)损失很多。
我使用这样的东西:
UIGraphicsBeginImageContext(image1.size);
[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
[image2 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
然而,它只增加了两个图像,它们的旋转,调整大小和移动都不在这里操作。任何人都可以帮助在编码中考虑这三个方面吗?任何帮助表示赞赏!
我最好的提前感谢:)
编辑:用户可以旋转和缩放图像(处理触摸事件)!
答案 0 :(得分:4)
您需要设置上下文的变换以匹配您开始绘制之前的imageView的变换。
即,
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, boundingRect.size.width/2, boundingRect.size.height/2);
transform = CGAffineTransformRotate(transform, angle);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
CGContextConcatCTM(context, transform);
// Draw the image into the context
CGContextDrawImage(context, CGRectMake(-imageView.image.size.width/2, -imageView.image.size.height/2, imageView.image.size.width, imageView.image.size.height), imageView.image.CGImage);
// Get an image from the context
rotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)];
并查看Creating a UIImage from a rotated UIImageView。
编辑:如果您不知道图像的旋转角度,您可以从UIImageView的图层属性获得转换:
UIGraphicsBeginImageContext(rotatedImageView.image.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform transform = rotatedImageView.transform;
transform = CGAffineTransformScale(transform, 1.0, -1.0);
CGContextConcatCTM(context, transform);
// Draw the image into the context
CGContextDrawImage(context, CGRectMake(0, 0, rotatedImageView.image.size.width, rotatedImageView.image.size.height), rotatedImageView.image.CGImage);
// Get an image from the context
UIImage *newRotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)];
UIGraphicsEndImageContext();
您必须使用变换矩阵来使图像在上下文中居中,您还必须为旋转的图像计算边界矩形,否则它将在角落处被裁剪(即rotateImageView.image.size不足以包含自身的旋转版本。)
答案 1 :(得分:0)
试试这个:
UIImage *temp = [[UIImage alloc] initWithCGImage:image1 scale:1.0 orientation: yourOrientation];
[temp drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
同样适用于image2。旋转和调整大小分别由方向和比例处理。 yourOrientation是一个UIImageOrientation枚举变量,可以有一个0-7的值(在不同的UIImageOrientation值上检查apple documentation)。希望它有所帮助...
编辑:要处理旋转,只需为所需的旋转编写所需的方向。您可以向左/向右旋转90度或垂直/水平翻转。例如,在Apple文档中,UIImageOrientationUp为0,UIImageOrientationDown为1,依此类推。查看我的github回购以获取示例。