Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Getting Image Size From a Path

Getting Image Size From a Path
Thread Tools
iOliverC
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
May 9, 2004, 07:23 AM
 
Hey guys,

I've got a text view that accepts drag and drop, basically, what I am wanting is when I drop an image file onto it, the size and width of it is made into a string and inserted into the text view. I can get the file path when it is dropped, but can't find out how to get the size (height and width) of the file dropped. I looked in NSFileManager and found something called contentsAtPath:, but I'm not sure if that is correct. Below is my current code:

Code:
- (BOOL)handleDroppedFile:(NSString *)filename forTextView:(NSTextView *)sender { [docTextView insertText:[NSString stringWithFormat:@"<img src=\"%@\" width=\"%@\" height=\"%@\" alt=\"\" />", filename]]; }
filename, is for example: /Volumes/Home/Users/olivercameron/Sites/images/header.png

Any help is appreciated,
Oliver
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 9, 2004, 08:08 AM
 
Create an NSImage with the file and get its size. Something along these lines:
Code:
- (BOOL)handleDroppedFile:(NSString *)filename forTextView:(NSTextView *)sender { NSSize imageSize; // I'm separating the format string just to make it more board-friendly NSString *formatString = @"<img src=\"%@\" width=\"%f\" height=\"%f\" alt=\"\" />"; NSImage *droppedImage = [[NSImage alloc] initWithContentsOfFile:filename]; if (!droppedImage) { return NO; } imageSize = [droppedImage size]; [droppedImage release]; // We only needed it to get the size [docTextView insertText:[NSString stringWithFormat:formatString, filename, imageSize.width, imageSize.height]]; }
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
iOliverC  (op)
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
May 9, 2004, 08:15 AM
 
Wow!

THanks a whole lot for the quick response, and the code works!

One little thing though, when I put an image thats 32 x 32, the code looks like this:

<img src="/Volumes/Home/Users/olivercameron/Sites/Talacia/images/codestyles.png" width="32.000000" height="32.000000" alt="" />

Note the .000000, i'm not quite sure how to get rid of this. Any idea?
     
iOliverC  (op)
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
May 9, 2004, 08:21 AM
 
Because this is kinda related with this, I have another little question .

I thought I knew how but I can't remember where I saw it, basically what I want to do is trim the file path. So it maybe only shows the last directory or so. So:

Volumes/Home/Users/olivercameron/Sites/Talacia/images/codestyles.png

Would only be:

images/codestyles.png

Any help is appreciated again :-).

Thanks,
Oliver
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 9, 2004, 04:30 PM
 
Originally posted by iOliverC:
I thought I knew how but I can't remember where I saw it, basically what I want to do is trim the file path. So it maybe only shows the last directory or so. So:

Volumes/Home/Users/olivercameron/Sites/Talacia/images/codestyles.png

Would only be:

images/codestyles.png
See the NSString method pathComponents. You can pick and choose which you want from there.

One little thing though, when I put an image thats 32 x 32, the code looks like this:

<img src="/Volumes/Home/Users/olivercameron/Sites/Talacia/images/codestyles.png" width="32.000000" height="32.000000" alt="" />

Note the .000000, i'm not quite sure how to get rid of this. Any idea?

It's standard printf formatting, so you can just change the places where it says %f to %.0f. That will tell it not to be precise to any decimal places.
( Last edited by Chuckit; May 10, 2004 at 03:28 PM. )
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
iOliverC  (op)
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
May 10, 2004, 01:04 PM
 
Thanks, the printing thing worked

Thanks
Oliver
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 08:07 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,