NSValueTransformerClass for NSImage from some enum/int value

85 Views Asked by At

How to use a transformable class to bind an NSImageView in NSTableView.

There is a flag of type enum, based on that the image needs to be changed.

1

There are 1 best solutions below

0
On BEST ANSWER

The NSValueTransformer class is as:

@implementation MyImageTransformer

+ (BOOL) allowsReverseTransformation{
    return NO;
}
+ (Class) transformedValueClass{
    return [NSImage class];
}


- (id) transformedValue:(id)value{

    NSArray *images = @[[NSImage imageNamed:@"failed.png"],
                        [NSImage imageNamed:@"success.png"],
                        [NSImage imageNamed:@"error.png"],
                        [NSImage imageNamed:@"inprogress.png"]
                        ];

    NSInteger integer = [value intValue];

    NSImage * image    = images[integer];
    NSData  * tiffData = [image TIFFRepresentation];

    return tiffData;

}
@end