Flip NSImage on both axes

后端 未结 4 682
醉酒成梦
醉酒成梦 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:00

    Anyway, I think a good idea should be posting a complete function. Here is my solution based on Ken post

    - (NSImage *)flipImage:(NSImage *)image
    {
        NSImage *existingImage = image;
        NSSize existingSize = [existingImage size];
        NSSize newSize = NSMakeSize(existingSize.width, existingSize.height);
        NSImage *flipedImage = [[[NSImage alloc] initWithSize:newSize] autorelease];
    
        [flipedImage lockFocus];
        [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
    
        NSAffineTransform *t = [NSAffineTransform transform];
        [t translateXBy:existingSize.width yBy:existingSize.height];
        [t scaleXBy:-1 yBy:-1];
        [t concat];
    
        [existingImage drawAtPoint:NSZeroPoint fromRect:NSMakeRect(0, 0, newSize.width, newSize.height) operation:NSCompositeSourceOver fraction:1.0];
    
        [flipedImage unlockFocus];
    
        return flipedImage;
    }
    

提交回复
热议问题