Search code examples
objective-ccocoaimage-processingbitmapnsimage

What is best way to get highest bitmap representation from NSImage object


I want to fetch highest NSBitmapImageRep from NSImage object.
For example if NSImage object contain:

NSIconRefBitmapImageRep 0x1272d0 Size={128, 128} ColorSpace=Generic RGB colorspace BPS=8 BPP=32 Pixels=128x128 Alpha=YES Planar=NO Format=0 CurrentBacking=nil (faulting),
NSIconRefBitmapImageRep 0x11b330 Size={256, 256} ColorSpace=Generic RGB colorspace BPS=8 BPP=32 Pixels=256x256 Alpha=YES Planar=NO Format=0 CurrentBacking=nil (faulting),
NSIconRefBitmapImageRep 0x19fe10 Size={512, 512} ColorSpace=Generic RGB colorspace BPS=8 BPP=32 Pixels=512x512 Alpha=YES Planar=NO Format=0 CurrentBacking=nil (faulting),
NSIconRefBitmapImageRep 0x124d10 Size={32, 32} ColorSpace=Generic RGB colorspace BPS=8 BPP=32 Pixels=32x32 Alpha=YES Planar=NO Format=0 CurrentBacking=nil (faulting),
NSIconRefBitmapImageRep 0x142180 Size={16, 16} ColorSpace=Generic RGB colorspace BPS=8 BPP=32 Pixels=16x16 Alpha=YES Planar=NO Format=0 CurrentBacking=nil (faulting)  

then i want {512 , 512} representation. I am using below code for this.

NSBitmapImageRep* requiredBitmap = nil;
    BOOL setValue =NO;

    NSEnumerator* imageEnum = [[origImage representations] objectEnumerator];

    while( imagerep = [imageEnum nextObject])
    {
        if ([imagerep isKindOfClass:[NSBitmapImageRep class]]) 
        {
            if (!setValue) {
                requiredBitmap = imagerep;
                setValue =YES;
            }
            if ([requiredBitmap pixelsHigh]<[imagerep pixelsHigh]) {
                requiredBitmap = imagerep;

                NSLog(@"%d", [imagerep pixelsHigh]);

            }
        }
    }


    NSImage* original512Image;

    if( requiredBitmap )
    {
        original512Image = [[NSImage alloc] initWithData:[requiredBitmap TIFFRepresentation]];


}  

Is there any efficient way to do this?


Solution

  • I am using

    id imageRep = nil;
    
    NSSize largImageSize;
    
    BOOL setValue =NO;
    
    NSEnumerator* imageEnum = [[lOriginalImage representations] objectEnumerator];
    
    while((imageRep = [imageEnum nextObject])) {
    
        if (!setValue) {
            largImageSize = [imageRep size];
            setValue =YES;
        }
        else if ((largImageSize.height) < ([imageRep size].height)) {
            largImageSize = [imageRep size];
        }
    
    }
    
    
    [lOriginalImage setSize:largImageSize];