I want to display image metadata using ios. Meta data like Aperture, Shutterspeed, Exposure Compensation, ISO, Lens Focal Length, etc. So please help me if anybody has idea
swift 4
static func imageDataProperties(_ imageData: Data) -> NSDictionary? {
if let imageSource = CGImageSourceCreateWithData(imageData as CFData, nil)
{
if let dictionary = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) {
return dictionary
}
}
return nil
}
Here is code, to get meta data from an image path:
NSData *imagedata = [NSData dataWithContentsOfFile:imagePath];
CGImageSourceRef source = CGImageSourceCreateWithData((CFMutableDataRef)imagedata, NULL);
NSDictionary *metadata = [(NSDictionary *)CGImageSourceCopyPropertiesAtIndex(source,0,NULL)autorelease];
Or, if using swift 4.0:
var imagedata = Data(contentsOfFile: imagePath)
var source: CGImageSourceRef = CGImageSourceCreateWithData((imagedata as? CFMutableDataRef), nil)
var metadata = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [AnyHashable: Any]
You need to use assets library to read exif metadata of image.
#import <AssetsLibrary/AssetsLibrary.h>
And then add following code :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block NSMutableDictionary *imageMetadata = nil;
[library assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
NSDictionary *metadata = asset.defaultRepresentation.metadata;
imageMetadata = [[NSMutableDictionary alloc] initWithDictionary:metadata];
NSLog(@"%@",imageMetadata.description);
}
failureBlock:^(NSError *error) {
}];
}
Hope this will help
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
if(referenceURL) {
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:referenceURL resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *rep = [asset defaultRepresentation];
NSDictionary *metadata = rep.metadata;
NSLog(@"image data %@", metadata);
} failureBlock:^(NSError *error) {
// error handling
}];
}
CGImageSourceRef source = CGImageSourceCreateWithURL( (CFURLRef) aUrl, NULL);
CGImageSourceRef source = CGImageSourceCreateWithData( (CFDataRef) theData, NULL);
NSDictionary* metadata = (NSDictionary *)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source,0,NULL));
CFRelease(source);
Check the dictionary contents here.
Reading image properties via Core Graphics wrapped as Swift struct:
struct ImageMetadata {
var imageProperties : [CFString: Any]
init?(data: Data) {
let options = [kCGImageSourceShouldCache: kCFBooleanFalse]
if let imageSource = CGImageSourceCreateWithData(data as CFData, options as CFDictionary),
let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as? [CFString: Any] {
self.imageProperties = imageProperties
} else {
return nil
}
}
var dpi : Int? { imageProperties[kCGImagePropertyDPIWidth] as? Int }
var width : Int? { imageProperties[kCGImagePropertyPixelWidth] as? Int }
var height : Int? { imageProperties[kCGImagePropertyPixelHeight] as? Int }
}