• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Determining image file types
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Determining image file types


  • Subject: Re: Determining image file types
  • From: Daniel Waylonis <email@hidden>
  • Date: Thu, 10 Jun 2004 17:00:00 -0700

On Jun 10, 2004, at 4:41 AM, Michael Becker wrote:

> How can I check a file whether it is a valid JPEG image?
> Right now, my application only accepts images which end with a .jpg or
> .jpeg. But if somebody messes up the ending, for example rename a
> PhotoShop file "theImage.psd" to "theImage.jpg", it will be loaded
> into my application as well. Actually this is not so much a problem at
> first, because NSImage seems to deal with any kind of image, but my
> application has to make SURE that it only accepts JPEG.

Hi Michael,

For the quickest (and dirtiest), you can also look at the first two
bytes of the file -- if they're not FFD8, it's probably not a JPEG.

For a much better and more robust (but slower) way, you can try to
create a CG data provider. If it succeeds, the image is the type you
expect.

For example (typed in Mail):

- (BOOL)isJPEGAtPath:(NSString *)path
{
NSURL *fileURL = [NSURL fileURLWithPath:path];
CGDataProviderRef dataProvider =
CGDataProviderCreateWithURL((CFURLRef)fileURL);
CGImageRef image = CGImageCreateWithJPEGDataProvider(dataProvider,
NULL, YES, kCGRenderingIntentDefault);
BOOL isJPEG = (image != NULL) ? YES : NO;

CGDataProviderRelease(dataProvider);
CGImageRelease(image);

return(isJPEG);
}

Dan
_________________________________________________
Dan Waylonis email@hidden
Software Engineer http://www.nekotech.com
nekotech SOFTWARE 650.964.2490 (O)
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >Determining image file types (From: Michael Becker <email@hidden>)

  • Prev by Date: My last stupid question, Random number generation.
  • Next by Date: Re: NSPipe
  • Previous by thread: Re: Determining image file types
  • Next by thread: HeaderDoc 8 vs. doxygen
  • Index(es):
    • Date
    • Thread