macos cocoa core-graphics nsimage

macos - NSImagen del tamaño no real con algunas imágenes?



cocoa core-graphics (4)

Veo que a veces el tamaño de NSImage no es de tamaño real (con algunas imágenes) y el tamaño de CIImage es siempre real. Estaba probando con esta image .

Este es el código fuente que escribí para probar:

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

Y la salida es:

Entonces, ¿cómo puede ser eso? Tal vez estoy haciendo algo mal?


Gracias a Zenopolis por el código ObjC original, aquí hay una bonita versión sucinta de Swift:

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))) }) }


Si su archivo contiene solo una imagen, puede usar esto:

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

la imagen es su NSImage, imageSize es el tamaño de la imagen en píxeles.

Copiado y actualizado aquí: https://.com/a/13228091/3608824


Tamaño de retorno del método de tamaño NSImage en puntos. Para obtener el tamaño representado en píxeles, debe inspeccionar la propiedad NSImage.representations que contiene una matriz de objetos NSImageRep con propiedades pixelWide / pixelHigh y un objeto NSImage de tamaño de cambio simple:

@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


NSImage método de size NSImage devuelve información de tamaño que depende de la resolución de pantalla. Para obtener el tamaño representado en la imagen de archivo real, necesita usar un NSImageRep . Puede obtener un NSImageRep de un NSImage utilizando el método de representations . Alternativamente, puede crear una instancia de subclase NSBitmapImageRep directamente como esta:

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

El ciclo tiene en cuenta que algunos formatos de imagen pueden contener más de una imagen (como TIFF, por ejemplo).

Puede crear un NSImage a este tamaño usando lo siguiente:

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