Note that there are some explanatory texts on larger screens.

plurals
  1. PONSImage Image Size With Multiple Layers
    primarykey
    data
    text
    <p>I have a <strong>Mac</strong> (not iOS) application that allows the user to select one or more images with <strong>NSOpenPanel</strong>. What I have trouble with is how to get correct dimensions for multiple-layered images. If an image contains one layer or compressed, the following will get me correct image dimensions with the file path.</p> <pre><code>NSImage *image0 = [[NSImage alloc] initWithContentsOfFile:path]; CGFloat w = image0.size.width; CGFloat h = image0.size.height; </code></pre> <p>But if I select an image that has multiple layers, I'll get strange numbers. For example, I have a single-layer image whose dimensions are 1,440 x 900 px according to <strong>Fireworks</strong>. If I add a small layer of a circle and save an image as <strong>PNG</strong> and read it, I get 1,458 x 911 px. According to <a href="https://stackoverflow.com/questions/9264051/nsimage-size-not-real-size-with-some-pictures">this topic</a> and <a href="https://stackoverflow.com/questions/3739165/nsimage-size-is-wrong">this topic</a>, they suggest that I read the largest layer. Okay. So I've created a function as follows.</p> <pre><code>- (CGSize)getimageSize :(NSString *)filepath { NSArray * imageReps = [NSBitmapImageRep imageRepsWithContentsOfFile:filepath]; NSInteger width = 0; NSInteger height = 0; for (NSImageRep * imageRep in imageReps) { if ([imageRep pixelsWide] &gt; width) width = [imageRep pixelsWide]; if ([imageRep pixelsHigh] &gt; height) height = [imageRep pixelsHigh]; } NSSize size = CGSizeMake((CGFloat)width, (CGFloat)height); return size; } </code></pre> <p>Using the function above, I get wrong dimensions (1,458 x 911 px) instead of 1,440 x 900 px. Actually, I had the same problem when I was developing Mac applications with REAL Stupid till a few years ago. So how can I get correct dimensions when an image contains multiple layers?</p> <p>Thank you for your advice.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload