什么是从NSImage对象获得最高位图表示的最佳方法

时间:2012-01-07 07:19:22

标签: objective-c cocoa image-processing bitmap nsimage

我想从NSImage对象中获取最高的NSBitmapImageRep 例如,如果NSImage对象包含:

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)  

然后我想要{512,512}表示。我正在使用下面的代码。

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


}  

有没有有效的方法可以做到这一点?

2 个答案:

答案 0 :(得分:0)

你应该使用快速枚举,但除此之外,你的方法与Leopard一样高效。

即便如此,循环遍历五个图像代表并检查其尺寸的开销将非常小,只需几个访问方法调用。

请记住,首先是个人资料,然后进行优化。先发制人优化是浪费时间。

NSBitmapImageRep* requiredBitmap = nil;
BOOL setValue =NO;

for(NSImageRep* imagerep in [origImage representations])
{
    if ([imagerep isKindOfClass:[NSBitmapImageRep class]]) 
    {
        if (!setValue) {
            requiredBitmap = imagerep;
            setValue =YES;
        }
        if ([requiredBitmap pixelsHigh]<[imagerep pixelsHigh]) {
            requiredBitmap = imagerep;

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

        }
    }
}


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

答案 1 :(得分:0)

我正在使用

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