NSImage size not real size with some pictures?

后端 未结 4 477
太阳男子
太阳男子 2020-12-02 20:53

I see that sometimes NSImage size is not real size (with some pictures) and CIImage size is always real. I was testing with this image.

This is source code which I w

4条回答
  •  不知归路
    2020-12-02 21:09

    NSImage size method returns size information that is screen resolution dependent. To get the size represented in the actual file image you need to use an NSImageRep. You can get an NSImageRep from an NSImage using the representations method. Alternatively you can create a NSBitmapImageRep subclass instance directly like this:

    NSArray * imageReps = [NSBitmapImageRep imageRepsWithContentsOfFile:@""];
    
    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);
    

    The loop takes into account that some image formats may contain more than a single image (such as TIFFs for example).

    You can create an NSImage at this size by using the following:

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

提交回复
热议问题