在两个轴上翻转NSImage [英] Flip NSImage on both axes

查看:304
本文介绍了在两个轴上翻转NSImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图翻转用NSImageBitmapRep表示创建的NSImage。经过一些挖掘( Flipping Quicktime预览和捕获)和

I'm trying to flip an NSImage created with a NSImageBitmapRep representation. After some digging (Flipping Quicktime preview & capture and Mirroring CIImage/NSImage) I tried two ways via a CIImage and applying a scaling transformation with -1 for both factors.

首先使用CIImage imageByApplyingTransform:

First using CIImage imageByApplyingTransform:

    NSBitmapImageRep *imgRep = ...
    CGImageRef cgi = [imgRep CGImage];
    CIImage *cii = [CIImage imageWithCGImage:cgi];
    CGAffineTransform at = CGAffineTransformTranslate(CGAffineTransformMakeScale(-1, -1), 0, 0);
    NSCIImageRep *ciiRep = [NSCIImageRep imageRepWithCIImage:[cii imageByApplyingTransform:at]];

    NSImage *img = [[[NSImage alloc] init] autorelease];
    [img addRepresentation:ciiRep];
    [self.ivImage setImage:img];

然后使用CIAffineTransform过滤器:

then using a CIAffineTransform filter:

    NSBitmapImageRep *imgRep = ...
    CGImageRef cgi = [imgRep CGImage];
    CIImage *cii = [CIImage imageWithCGImage:cgi];
    CIFilter *f = [CIFilter filterWithName:@"CIAffineTransform"];
    NSAffineTransform *t = [NSAffineTransform transform];
    [t scaleXBy:1.0 yBy:1.0];
    //[t translateXBy:width yBy:0];
    [f setValue:t forKey:@"inputTransform"];
    [f setValue:cii forKey:@"inputImage"];
    CIImage *cii2 = [f valueForKey:@"outputImage"];
    NSCIImageRep *ciiRep = [NSCIImageRep imageRepWithCIImage:cii2];

    NSImage *img = [[[NSImage alloc] init] autorelease];
    [img addRepresentation:ciiRep];
    [self.ivImage setImage:img];

两种方法都会产生空白图像。我也试图翻译图像,万一它是在屏幕外,因为-1缩放,但没有效果。如果我使用正值缩放,我可以看到转换应用正确。

Both ways produce a blank image. I've also tried to translate the image, in case it is out of screen because of the -1 scalings, but to no avail. If I use positive values for scaling I can see that the transformation is applied correctly.

self.ivImage是一个NSImageView。我想要一个实际的NSImage翻转,所以应用一个转换到NSImageView是不是一个选项。

self.ivImage is an NSImageView. I want an actual NSImage which is flipped, so applying a transformation to the NSImageView is not an option.

这是32位,狮子会上的Xcode 4.3.2。 >

This is 32bits, Xcode 4.3.2 on Lion.

推荐答案

您应该使用大小启动 NSImage

显示您的翻译尝试,因为这是正确的方法。通常,最容易先翻译,然后缩放。你的代码片段似乎有残留的尝试翻译的痕迹,但他们是不对的。在一个案例中翻译为0,0,在另一个案例中翻译为0。此外,在第二个代码段中,您缩放为1,1(正),因此不会翻转。

Show your attempt with translation, because that's the right way. Typically, it's easiest to translate first and then scale. Your code snippets seem to have vestigial traces of attempts to translate, but they're not right. You translate by 0,0 in one case and width,0 in another. Also, in your second code snippet, you're scaling by 1,1 (positive), so not flipping.

此外,简单地将焦点锁定一个适当大小的新图像,设置变换,并绘制图像代表。使用 CIImage 避开所有这些东西。

Also, it may be simpler to simply lock focus on a new image of the appropriate size, set up the transform, and draw the image rep. Avoids all of that stuff with CIImage.

NSBitmapImageRep *imgRep = ...
NSImage* img = [[[NSImage alloc] initWithSize:NSMakeSize(imgRep.pixelsWide, imgRep.pixelsHigh)] autorelease];
[img lockFocus];
NSAffineTransform* t = [NSAffineTransform transform];
[t translateXBy:imgRep.pixelsWide yBy:imgRep.pixelsHigh];
[t scaleXBy:-1 yBy:-1];
[t concat];
[imgRep drawInRect:NSMakeRect(0, 0, imgRep.pixelsWide, imgRep.pixelsHigh)];
[img unlockFocus];

这篇关于在两个轴上翻转NSImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆