2012-02-13 19 views
24

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

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

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:

enter image description here

Así que cómo puede ser ?? Tal vez estoy haciendo algo mal?

+4

Gracias por el gran fondo de pantalla! – JustSid

+0

@JustSid De nada :) –

Respuesta

38

NSImagesize método 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 desde NSImage usando el método representations. Alternativamente, puede crear una instancia de NSBitmapImageRep subclase directamente como esto:

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 bucle tiene en cuenta que algunos formatos de imagen pueden contener más de una sola imagen (tal como archivos TIFF, por ejemplo).

puede crear una NSImage en este tamaño utilizando la siguiente:

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

Gracias, pero una pregunta más. Ahora sé tamaño real, pero NSImage es pequeño en ese tamaño ... ¿cómo evitar esto? Necesito usar NSImage. Por ejemplo, cambio el marco de NSView al tamaño real de la imagen, pero en ese marco el NSImage se dibuja pequeño. –

+0

He realizado algunos cambios en la respuesta que deberían ayudar. – zenopolis

+0

Eso funciona bien ahora, gracias! –

5

método size NSImage tamaño de retorno en puntos. Para obtener el tamaño representado en píxeles que necesita inspeccionar la propiedad NSImage.representations que contiene un conjunto de objetos con propiedades NSImageRep pixelWide/pixelHigh y sencillo cambiar el tamaño NSImage objeto:

@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

Gracias a Zenopolis por el código original ObjC, he aquí una agradable concisa versión 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))) 
     }) 
    } 
0

Si el archivo contiene sólo una imagen, sólo puede utilizar esta: imagen

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

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

copia y se actualizan aquí: https://stackoverflow.com/a/13228091/3608824

Cuestiones relacionadas