Flip NSImage on both axes

后端 未结 4 679
醉酒成梦
醉酒成梦 2021-01-12 10:47

I\'m trying to flip an NSImage created with a NSImageBitmapRep representation. After some digging (Flipping Quicktime preview & capture and Mirroring CIImage/NSImage) I

4条回答
  •  伪装坚强ぢ
    2021-01-12 11:22

    You should init your NSImage with a size.

    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.

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

提交回复
热议问题