【发布时间】:2012-01-07 07:19:22
【问题描述】:
我想从 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]];
}
有什么有效的方法吗?
【问题讨论】:
-
你试过
bestRepresentationForRect:context:hints:有一个非常大的矩形(可能是CGRectInfinite)吗? -
此方法在 Mac OS X v10.6 及更高版本中可用。我正在寻找 Leopard os 的解决方案。
标签: objective-c cocoa image-processing bitmap nsimage