How to get image metadata in ios
22,211
Solution 1
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.
Solution 2
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]
Solution 3
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
}
Solution 4
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 }
}
Solution 5
-(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
}];
}
Author by
Hardik Shah
Updated on April 22, 2021Comments
-
Hardik Shah about 3 years
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.