Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to fetch meta data of an image in iOS

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 4.00k
    Comment on it

    Metadata can be described as data about data here the metadata of an image we will fetch by using code in IOS9 . Metadata of an image includes resolution,gps location,creation date,modification date etc. here we will discuss a very simple way to find the metadata of an image selected from the gallery of phone. First of all we have to take an image of which we have to find metadata so this we will do with the help of UIImagePickerController. We will create a function by which we will select a picture before starting with function first of all set these two delegates in a view controller to use UIImagePickerController which are as follows:-

    <UIImagePickerControllerDelegate,UINavigationControllerDelegate>

    
    - (void)selectPictureFromGallery:(UIViewController *)controller{
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];//allocating  memory to UIImagePickerController object
            picker.delegate =self; //set delegate of UIImagePickerController 
            picker.allowsEditing = YES;
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//take images from gallery
            [controller presentViewController:picker animated:YES completion:NULL];// picker is added to the present controller
    
    }
    



    After picking the image from gallery now we have to fetch the metadata of that particular image so we will use below default method

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
        
         NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];// fetch url of selected image
        [self metadata:referenceURL];// calling the metadata function for fetching the metadata
        [picker dismissViewControllerAnimated:YES completion:NULL];
        
    }


     

    Now the method for fetching the metadata.Here we are using PHAsset class to fetch the url of particular image selected from picker so to use PHAsset class we have to import one file which is given below:-

    #import <Photos/Photos.h> //to use PHAsset class in program
    -(void)metadata:(NSURL *)url
    {
        
        PHAsset *asset=[PHAsset fetchAssetsWithALAssetURLs:@[url] options:nil].firstObject;
        if (asset) {
            // get photo info from this asset
            PHImageRequestOptions * imageRequestOptions = [[PHImageRequestOptions alloc] init];
            imageRequestOptions.synchronous = YES;
            [[PHImageManager defaultManager]
             requestImageDataForAsset:asset
             options:imageRequestOptions
             resultHandler:^(NSData *imageData, NSString *dataUTI,
                             UIImageOrientation orientation,
                             NSDictionary *info)
             {
                 NSDictionary *dict = [self metadataFromImageData:imageData];// as this imageData is in NSData format so we need a method to convert this NSData into NSDictionary to display metadata
    
             }];
        }  
        
    }


     

    Here is the method which will convert the NSdata into NSDictionary

    -(NSDictionary*)metadataFromImageData:(NSData*)imageData{
        CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)(imageData), NULL);
        if (imageSource) {
            NSDictionary *options = @{(NSString *)kCGImageSourceShouldCache : [NSNumber numberWithBool:NO]};
            CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (__bridge CFDictionaryRef)options);
            if (imageProperties) {
                NSDictionary *metadata = (__bridge NSDictionary *)imageProperties;
                CFRelease(imageProperties);
                CFRelease(imageSource);
               NSLog(@"Metadata of selected image%@",metadata);// It will display the metadata of image after converting NSData into NSDictionary
                return metadata;
                
            }
            CFRelease(imageSource);
        }
        
        NSLog(@"Can't read metadata");
        return nil;
    }



    In this way we can find the metadata of a particular image selected from the gallery of phone.

    Output:-

    2016-05-16 14:49:06.623  Metadata of image = {
        ColorModel = RGB;
        DPIHeight = 72;
        DPIWidth = 72;
        Depth = 8;
        Orientation = 1;
        PixelHeight = 2848;
        PixelWidth = 4288;
        ProfileName = "Adobe RGB (1998)";
        "{Exif}" =     {
            ColorSpace = 1;
            ComponentsConfiguration =         (
                1,
                2,
                3,
                0
            );
            DateTimeDigitized = "2009:10:09 14:09:20";
            DateTimeOriginal = "2009:10:09 14:09:20";
            ExifVersion =         (
                2,
                2
            );
            ExposureBiasValue = 0;
            ExposureProgram = 3;
            ExposureTime = "0.005";
            FNumber = "5.6";
            Flash = 0;
            FlashPixVersion =         (
                1,
                0
            );
            FocalLength = 105;
            ISOSpeedRatings =         (
                400
            );
            LightSource = 9;
            MaxApertureValue = 5;
            MeteringMode = 5;
            PixelXDimension = 4288;
            PixelYDimension = 2848;
            SceneCaptureType = 0;
            SensingMethod = 2;
        };
        "{IPTC}" =     {
            DateCreated = 20091009;
            DigitalCreationDate = 20091009;
            DigitalCreationTime = 140920;
            ObjectName = "DSC_0010";
            TimeCreated = 140920;
        };
        "{JFIF}" =     {
            DensityUnit = 1;
            JFIFVersion =         (
                1,
                0,
                1
            );
            XDensity = 72;
            YDensity = 72;
        };
        "{TIFF}" =     {
            DateTime = "2009:10:18 16:49:28";
            Make = "NIKON CORPORATION";
            Model = "NIKON D90";
            Orientation = 1;
            PhotometricInterpretation = 2;
            ResolutionUnit = 2;
            Software = "QuickTime 7.6.3";
            XResolution = 72;
            YResolution = 72;
        };
    }
    
    
    

     

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: