Search code examples
ioscocoa-touchimage-processingios7exif

How to read complete exif and tiff info of image in ios


i am trying to extract exif info from image .

UIImage *pImage = [UIImage imageNamed:@"IMG_0011.JPG"];
NSData* pngData =  UIImageJPEGRepresentation(pImage, 1.0);

CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)pngData, NULL);
NSDictionary *metadata = (__bridge NSDictionary *) CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
NSLog(@"exif %@" , metadata);

i got following metadata info

exif {
    ColorModel = RGB; <br>
    Depth = 8; <br>
    Orientation = 1; <br>
    PixelHeight = 1936;<br>
    PixelWidth = 2592;<br>
    "{Exif}" =     {
        ColorSpace = 1;<br>
        PixelXDimension = 2592;<br>
        PixelYDimension = 1936;<br>
    };
    "{JFIF}" =     {<br>
        DensityUnit = 0;<br>
        JFIFVersion =         (<br>
            1,<br>
            1<br>
        );
        XDensity = 1;<br>
        YDensity = 1;<br>
    };
    "{TIFF}" =     {<br>
        Orientation = 1;<br>
    };
}<br>

But when i check the image using exif data viewer or preview app following info i got.

Aperture Value: 2.526<br>
Brightness Value: 1.897<br>
Color Space: sRGB<br>
Components Configuration: 1, 2, 3, 0<br>
Date Time Digitized: 20-Dec-2013 1:33:31 PM<br>
Date Time Original: 20-Dec-2013 1:33:31 PM<br>
Exif Version: 2.2.1<br>
Exposure Mode: Auto exposure<br>
Exposure Program: Normal program<br>
Exposure Time: 1/15<br>
Flash: No flash function<br>
FlashPix Version: 1.0<br>
FNumber: 2.4
Focal Length: 4.28<br>
Focal Length In 35mm Film: 35<br>
ISO Speed Ratings: 125<br>
Metering Mode: Pattern<br>
Pixel X Dimension: 2,592<br>
Pixel Y Dimension: 1,936<br>
Scene Capture Type: Standard<br>
Sensing Method: One-chip color area sensor<br>
Shutter Speed Value: 3.907
Subject Area: 1295, 967, 699, 696
White Balance: Auto white balance<br>
Regions: {
    HeightAppliedTo = 1936;<br>
    RegionList =     (
                {
            ConfidenceLevel = 326;<br>
            FaceID = 3;<br>
            Height = "0.172";<br>
            Timestamp = 956449648;<br>
            Type = Face;<br>
            Width = "0.128";
            X = "0.542";<br>
            Y = "0.426";<br>
        }
    );
    WidthAppliedTo = 2592; <br>
}

any suggestion how to extract full exif data on ios ....?


Solution

  • The issue is that you're round-tripping this through UIImage and converting to a NSData via UIImageJPEGRepresentation, which will strip lots of interesting information. Just load the NSData directly:

    NSString *path = [[NSBundle mainBundle] pathForResource:@"IMG_0011" ofType:@"JPG"];
    NSData *pngData = [NSData dataWithContentsOfFile:path];
    
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)pngData, NULL);
    NSDictionary *metadata = CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source, 0, NULL));
    NSLog(@"exif %@" , metadata);
    CFRelease(source);
    

    BTW, don't forget the "Create Rule", namely that Core Foundation functions with Create or Copy in their name are transferring ownership to you, and you're responsible for releasing the objects, yourself. For example, you can use __bridge_transfer or CFBridgingRelease to transfer ownership of the source properties dictionary to ARC. And you can explicitly CFRelease of the CGImageSourceRef.