2014-09-28 106 views
0

对于给定的UIImage,我的目标是生成另一个UIImage,它是原始图像的对角镜像版本。我正在使用Core Graphics来实现这一点。iOS:如何使用核心图形对角镜像图像

我的尝试是,以生成仿射变换矩阵为对角镜操作如下:

| 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; 
} 

与变换矩阵的问题?或者我如何使用Core Graphics?

回答

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; 
    }