对于给定的UIImage,我的目标是生成另一个UIImage,它是原始图像的对角镜像版本。我正在使用Core Graphics来实现这一点。
我的尝试是为对角镜像操作生成仿射变换矩阵,如下所示:
| 0 1 0 |
| 1 0 0 |
| 0 0 1 |
但是得到的图像是旋转的图像,而不是对角镜像......
更具体地说,这是我尝试过的代码:
+ (UIImage *)mirrorImageDiagonal:(UIImage *)fromImage
{
CGContextRef mainViewContentContext = MyCreateBitmapContext(fromImage.size.width, fromImage.size.height);
CGAffineTransform transform = CGAffineTransformIdentity;
transform.a = 0;
transform.b = 1;
transform.c = 1;
transform.d = 0;
transform.tx = 0;
transform.ty = 0;
CGContextConcatCTM(mainViewContentContext, transform);
// draw the image into the bitmap context
CGContextDrawImage(mainViewContentContext, CGRectMake(0, 0, fromImage.size.width, fromImage.size.height), fromImage.CGImage);
// create CGImageRef of the main view bitmap content, and then release that bitmap context
CGImageRef reflectionImage = CGBitmapContextCreateImage(mainViewContentContext);
CGContextRelease(mainViewContentContext);
// convert the finished reflection image to a UIImage
UIImage *theImage = [UIImage imageWithCGImage:reflectionImage];
// image is retained by the property setting above, so we can release the original
CGImageRelease(reflectionImage);
return theImage;
}
转换矩阵存在问题吗?或者我如何使用核心图形?
答案 0 :(得分:0)
您正在使用的矩阵将应用关于坐标对角线的变换,而不是关于图像的对角线。 (除非图像具有方形尺寸,在这种情况下它们是相同的轴)。
你得到的图像然而它似乎是一个旋转,但实际上它是一个倒像;
+ (UIImage *)mirrorImageDiagonal1:(UIImage *)fromImage{
//large square (max(h,w),max(h,w))
CGSize neededSize = CGSizeMake(fromImage.size.height, fromImage.size.height);//should be max(h,w)
UIGraphicsBeginImageContext(mirrorSize);
CGContextRef context = UIGraphicsGetCurrentContext();
// Tanslate and scale upside-down to compensate for Quartz's inverted coordinate system
CGContextTranslateCTM(context, 0, fromImage.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGAffineTransform transform = CGAffineTransformIdentity;
transform.a = 0;
transform.b = 1;
transform.c = 1;
transform.d = 0;
transform.tx = 0;
transform.ty = 0;
//transform = CGAffineTransformInvert(transform);
CGContextConcatCTM(context, transform);
// draw the image into the bitmap context
CGContextDrawImage(context, CGRectMake(0,0 , fromImage.size.width, fromImage.size.height), fromImage.CGImage);
// create CGImageRef of the main view bitmap content, and then release that bitmap context
CGImageRef reflectionImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
// convert the finished reflection image to a UIImage
UIImage *theImage = [UIImage imageWithCGImage:reflectionImage];
// image is retained by the property setting above, so we can release the original
CGImageRelease(reflectionImage);
return theImage;
}