2012-02-13 124 views
24

我看到有時NSImage大小不是實際大小(有一些圖片),CIImage大小總是真實的。我正在測試這imageNSImage尺寸與一些圖片不是實際尺寸?

這是我寫的測試代碼:

NSImage *_imageNSImage = [[NSImage alloc]initWithContentsOfFile:@"<path to image>"]; 

NSSize _dimensions = [_imageNSImage size]; 

[_imageNSImage release]; 

NSLog(@"Width from CIImage: %f",_dimensions.width); 
NSLog(@"Height from CIImage: %f",_dimensions.height); 




NSURL *_myURL = [NSURL fileURLWithPath:@"<path to image>"]; 
CIImage *_imageCIImage = [CIImage imageWithContentsOfURL:_myURL]; 


NSRect _rectFromCIImage = [_imageCIImage extent]; 

NSLog(@"Width from CIImage: %f",_rectFromCIImage.size.width); 
NSLog(@"Height from CIImage: %f",_rectFromCIImage.size.height); 

和輸出是:

enter image description here

所以怎麼說也?也許我做錯了什麼?

+4

感謝您的偉大壁紙! – JustSid 2012-02-13 17:53:20

+0

@JustSid不客氣:) – 2012-02-13 18:04:45

回答

38

NSImagesize方法返回屏幕分辨率相關的大小信息。要獲取實際文件圖像中顯示的大小,您需要使用NSImageRep。您可以使用representations方法從NSImage獲得NSImageRep。或者,也可以像這樣直接創建NSBitmapImageRep子類實例:

NSArray * imageReps = [NSBitmapImageRep imageRepsWithContentsOfFile:@"<path to image>"]; 

NSInteger width = 0; 
NSInteger height = 0; 

for (NSImageRep * imageRep in imageReps) { 
    if ([imageRep pixelsWide] > width) width = [imageRep pixelsWide]; 
    if ([imageRep pixelsHigh] > height) height = [imageRep pixelsHigh]; 
} 

NSLog(@"Width from NSBitmapImageRep: %f",(CGFloat)width); 
NSLog(@"Height from NSBitmapImageRep: %f",(CGFloat)height); 

環路考慮到某些圖象格式可以包含除單個圖像多個(諸如TIFF格式例如)。

您可以通過以下這個尺寸創建一個NSImage中:在點

NSImage * imageNSImage = [[NSImage alloc] initWithSize:NSMakeSize((CGFloat)width, (CGFloat)height)]; 
[imageNSImage addRepresentations:imageReps]; 
+0

謝謝,但還有一個問題。現在我知道真正的大小,但是NSImage在這個尺寸上很小。如何避免這種情況?我需要使用NSImage。例如,我將NSView幀更改爲實際大小的圖像,但在該幀中,NSImage被縮小。 – 2012-02-14 08:49:13

+0

我對應該幫助的答案做了一些更改。 – zenopolis 2012-02-14 11:45:39

+0

現在很好,謝謝! – 2012-02-14 12:15:16

5

NSImage中尺寸方法返回大小。若要像素大小代表你需要檢查包含NSImageRep的陣列pixelWide/pixelHigh性能和簡單的改變大小NSImage中對象的對象NSImage.representations屬性:

@implementation ViewController { 
    __weak IBOutlet NSImageView *imageView; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do view setup here. 

    NSImage *image = [[NSImage alloc] initWithContentsOfFile:@"/Users/username/test.jpg"]; 

    if (image.representations && image.representations.count > 0) { 
     long lastSquare = 0, curSquare; 
     NSImageRep *imageRep; 
     for (imageRep in image.representations) { 
      curSquare = imageRep.pixelsWide * imageRep.pixelsHigh; 
      if (curSquare > lastSquare) { 
       image.size = NSMakeSize(imageRep.pixelsWide, imageRep.pixelsHigh); 
       lastSquare = curSquare; 
      } 
     } 

     imageView.image = image; 
     NSLog(@"%.0fx%.0f", image.size.width, image.size.height); 
    } 
} 

@end 
5

感謝Zenopolis原始ObjC代碼,這裏有一個漂亮簡潔斯威夫特版本:

func sizeForImageAtURL(url: NSURL) -> CGSize? { 
     guard let imageReps = NSBitmapImageRep.imageRepsWithContentsOfURL(url) else { return nil } 
     return imageReps.reduce(CGSize.zero, combine: { (size: CGSize, rep: NSImageRep) -> CGSize in 
      return CGSize(width: max(size.width, CGFloat(rep.pixelsWide)), height: max(size.height, CGFloat(rep.pixelsHigh))) 
     }) 
    } 
0

如果你的文件只包含一個圖像,你可以這樣做:

let rep = image.representations[0] 
let imageSize = NSSize(width: rep.pixelsWide, height: rep.pixelsHigh) 

像我是你的NSImage,imageSize是以像素爲單位的圖像大小。

複製並更新此處:https://stackoverflow.com/a/13228091/3608824