0

I have as an input the dump of an image in an NSData object. Now, I want to extract relevant information of the image from this object like number of pixels, no. of bits per pixel, etc.

Can anyone tell me how to extract this info from the NSData object dump?

P.S.: I have gone through this documentation of the NSData class, but could not isolate out the relevant methods.

Sankalp
  • 2,796
  • 3
  • 30
  • 43

2 Answers2

1

So the easiest way is to actually build the UIImage object from the NSData then extract the info from the UIImage then.

UIImage* image = [UIImage imageWithData:yourData];
NSLog(@"Image is %dx%d",image.size.width, image.size.height);

If you are only interested in the properties of the image but don't want to actually build its representation and only get the properties, take a look at CGImageSource

#import <ImageIO/ImageIO.h>

CGImageSourceRef imgSrc = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
size_t nbImages = CGImageSourceGetCount(imgSrc);
for(size_t idx=0; idx<nbImages; ++idx)
{
    NSDictionary* props = (__bridge NSDictionary*)CGImageSourceCopyPropertiesAtIndex(imgSrc, idx, NULL);
    NSLog(@"properties for image %lu in imageSource: %@", idx, props);
}
CFRelease(imgSrc);

[EDIT] For this to work, obviously add the ImageIO.framework to your "Link Binary With Libraries" How to add ImageIO framework

AliSoftware
  • 32,623
  • 6
  • 82
  • 77
  • Will I be able to get the relevant pixel level information from the `UIImage`? I cannot assume, as mentioned in the post referred to by @Zsolt, anything like the number of bits per pixel. – Sankalp Sep 26 '13 at 09:30
  • I'll give `CGImageSourceRef` a try. Thanks. – Sankalp Sep 26 '13 at 09:38
  • I edited my code with `CGImageSource` so it now loop thru all the images in the imageSource, as `CGImageSourceCopyProperties` copy the properties of the image source only (you will probably only have the file size in those), whereas an imageSource is composed with one or more images (for example TIFF or GIF formats can contain multiple images in one file) and you have to loop thru each to get the properties of each image using `CGImageSourceCopyPropertiesAtIndex` (which will return the `Depth`, `ColorModel`, `PixelHeight`, `PixelWidth`, …) – AliSoftware Sep 26 '13 at 09:52
  • Great! But, I am unable to import the `ImageIO/CGImageSource.h` header file into my project (There is no `ImageIO/ImageIO.h` file in my MAC sdk. I can see the file, but when I import it, the build fails saying- "file not found". Any help here? – Sankalp Sep 26 '13 at 10:04
  • I thought this would go without saying seeing the format of the `#import`: `ImageIO` is a framework so you have to add the `ImageIO.framework` to your project (select the project on the left, then your target, go to the Build Phases tab and under "Link Binary With Libraries" click on the "+" to add the framework) – AliSoftware Sep 26 '13 at 10:13
  • I added the `ImageIO.framework` and also its parent framework. Still, the same error after restarting Xcode. – Sankalp Sep 26 '13 at 10:33
  • Do I also need to configure the `header search path` in the `Build settings` tab? – Sankalp Sep 26 '13 at 10:39
  • No, you don't have to add anything to the "Header Search Path". Adding a framework to your project is a very common action. You only have to add the framework to the Link with Binaries phase. If you never did this, maybe refer to Xcode help, or to other generic SO questions about adding a framework to a project (this is not related to ImageIO.framework). _By the way, what do you mean by "also its parent framework" ? frameworks don't have "parents"… are you sure you added the right thing ?!_ – AliSoftware Sep 26 '13 at 11:53
  • I added a capture to show you I to do it. Just tested myself on my sandbox project I use to test various things, I confirm you shouldn't have to do anything more than that to use `CGImageSourceXXX` functions from that framework. – AliSoftware Sep 26 '13 at 12:02
  • I even created a brand new OSX project, added the framework, copy/pasted my code above, and it works like a charm too. Make sure you added the framework correctly and wrote the `#import ` correctly, with brackets and all (like we usually do for any other frameworks) – AliSoftware Sep 26 '13 at 12:14
  • Thanks for helping out! I had done it exactly the way you mentioned in the capture.. But, I am still getting "file not found" error. So, for now, I am moving ahead with things by providing the full path to include the header files. BTW, by parent framework, I meant - `ApplicationServices.framework` under which `ImageIO.framework` is contained. – Sankalp Sep 26 '13 at 14:19
  • But, have now removed the `ApplicationServices.framework` link from the project, while keeping the `ImageIO.framework`. Will check later why the hell is the file not being imported.. – Sankalp Sep 26 '13 at 14:20
  • You did add it to the correct target, right (especially your app target and not the unit tests target for example)? – AliSoftware Sep 26 '13 at 18:47
0

Convert the data to UIImage, then take a look at this post.

UIImage *image = [UIImage imageWithData:data];
Community
  • 1
  • 1
Lord Zsolt
  • 6,492
  • 9
  • 46
  • 76
  • In the code mentioned in the post, the pixel level information has been assumed to be something standard to get the color components. I need to extract, not assume it. – Sankalp Sep 26 '13 at 09:31