objective-cmacosnstableviewcocoa-bindingsnsvaluetransformer

NSValueTransformerClass for NSImage from some enum/int value


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.


Solution

  • 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