Re: getting info of an image
Re: getting info of an image
- Subject: Re: getting info of an image
- From: Hayden Stainsby <email@hidden>
- Date: Sat, 31 Dec 2005 01:30:46 +0000
On 29 Dec 2005, at 12:18, Camillo Lugaresi wrote:
On 29/dic/05, at 13:04, Francesca wrote:
Hallo,
in my apps I load various images.
Now, I would get the info of my image, as resolution, height,
with, creator... etc etc.
Now i done this with applescript.
I tell ImageEvent to get the MetaData.
But,
I would know if there is a way by cocoa....
I'm searching in FileManager,
I've found various constant, such as NSFileSize and others.
But not all what I need.
Someone can help me???
Can I use NSImage or what to get all the metaData of my file???
If you want to get all the metadata that Spotlight supports, use
MDItem in Carbon. Cocoa has NSMetadataItem, but the only way to
obtain one seems to be through a query, so if you want information
about a specific file MDItem is the way to go. You don't have to
link with the entire Carbon framework to use MDItem, just link with
CoreServices.
Camillo
Quick note before we begin to all the veterans out there. Has anyone
written an objective-c class to do all the Core Graphics file reading
stuff (for an example see code below). It'd be nice if you could just
grab a class with all of this wrapped up into nice, easy to use
methods, just a thought.
You can also use Core Graphics to access all the image properties.
I've just spent the last few hours getting to grips with it myself,
but here is and example method that should help code that should get
you on your way:
(Note: this is mostly adapted from the ImageApp example which can be
found here:
http://developer.apple.com/samplecode/ImageApp/
ImageApp.html)
- (void)getImagePropertiesFromPath:(NSString *)path
{
CGImageRef imageRef;
CGImageSourceRef imageSourceRef;
CFDictionaryRef *metaData;
NSURL *urlOfImage = [[NSURL alloc] initFileURLWithPath:path];
NSDictionary *imageOptions = [NSDictionary
dictionaryWithObjectsAndKeys:
(id)kCFBooleanTrue, (id)kCGImageSourceShouldCache,
(id)kCFBooleanTrue, (id)kCGImageSourceShouldAllowFloat,
nil];
CFNumberRef pixelHeight, pixelWidth, dpiHeight, dpiWidth;
CFStringRef colourModel
imageSourceRef = CGImageSourceCreateWithURL((CFURLRef)urlToImage,
NULL);
imageRef = CGImageSourceCreateImageAtIndex(imageSourceRef, 0,
(CFDictionaryRef)imageOptions);
metaData = CGImageSourceCopyPropertiesAtIndex(imageSourceRef, 0,
(CFDictionaryRef)imageOptions);
pixelWidth = (CFNumberRef)[(NSDictionary *)metaData objectForKey:(id)
kCGImagePropertyPixelWidth];
pixelHeight = (CFNumberRef)[(NSDictionary *)metaData objectForKey:
(id)kCGImagePropertyPixelHeight];
dpiWidth = (CFNumberRef)[(NSDictionary *)metaData objectForKey:(id)
kCGImagePropertyDPIWidth];
dpiHeight = (CFNumberRef)[(NSDictionary *)metaData objectForKey:(id)
kCGImagePropertyDPIHeight];
colourModel = (CFNumberRef)[(NSDictionary *)metaData objectForKey:
(id)kCGImagePropertyColorModel];
}
There are a load of other kCGImageProperty... keys for the dictionary
containing the meta data, you can look them up on developer.apple.com
or within the Reference Library that's built into XCode. The
important thing to remember though is that each one returns a
different type of value, and they're all CF (Core Foundation) types,
not NS, so you may have to play around a little to make them produce
an output that you can use.
What this won't give you is any file system information (such as date
created, HFS creator code etc.). To get those values you'll need to
go to do something like this (assuming that path has a full path of a
file in it):
NSFileManager *myFileManager = [NSFileManager defaultManager];
NSDictionary *myFileAttributes = [myFileManager
fileAttributesAtPath:path traverseLink:TRUE];
NSString *myTypeCode = NSFileTypeForHFSTypeCode([myFileAttributes
fileHFSTypeCode]);
NSString *myCreatorCode = NSFileTypeForHFSTypeCode(myFileAttributes
fileHFSCreatorCode]);
unsigned long long *myFileSize = [myFileAttributes fileSize];
Again, you can look up the functions, classes and methods here in the
Apple documentation. And again, you need to be careful of what is
actually being returned by these functions, although most are in more
convenient forms for use with objective-c.
Also I don't endorse assigning values to all your variables when you
initialise them, I did it here to help with the readability. In
general it makes your code horribly messy and top heavy, and looks bad.
Anyway, hope this helps.
--
Hayden
#!/usr/bin/perl
chop($_=<>);@s=split/ /;foreach$m(@s){if($m=='*'){$z=pop@t;$x
=pop@t;push@t,$a=eval"$x$m $z";}else{push@t,$m;}}print"$a\n";
#
http://helisse.ath.cx:8080/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden