|
|
Getting Image Size From a Path
|
|
|
|
Grizzled Veteran
Join Date: Jun 2002
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
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'."
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Jun 2002
Status:
Offline
|
|
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?
|
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Jun 2002
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
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'."
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Jun 2002
Status:
Offline
|
|
Thanks, the printing thing worked
Thanks
Oliver
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|