How to Get Image Metadata in iOS

How to get image metadata in ios


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.

How to get image metadata in iOS 9 using Photos Framework

At first, you can get info[UIImagePickerControllerReferenceURL] from UIImagePickerController. Then you can do things like the following:

let assetURL = info[UIImagePickerControllerReferenceURL] as! NSURL
let asset = PHAsset.fetchAssetsWithALAssetURLs([assetURL], options: nil)
guard let result = asset.firstObject where result is PHAsset else {
return
}

let imageManager = PHImageManager.defaultManager()
imageManager.requestImageDataForAsset(result as! PHAsset, options: nil, resultHandler:{
(data, responseString, imageOriet, info) -> Void in
let imageData: NSData = data!
if let imageSource = CGImageSourceCreateWithData(imageData, nil) {
let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)! as NSDictionary
//now you have got meta data in imageProperties, you can display PixelHeight, PixelWidth, etc.
}
})

Is there way to get metadata of the image file in iOS?

You can use the ImageIO Framework like explained in QA1654:

To access the image properties you:

  1. create a CGImageSourceRef for your image.
  2. get a copy of the image properties dictionary by calling CGImageSourceCopyPropertiesAtIndex.
  3. access the values in the dictionary by calling CFDictionaryGetValue with the property key that you are interested in. The available keys
    are located in the CGImageProperties Reference.

Get UIImage metadata without using UIImagePickerController

You might find this a useful starting point…

func getMetaData(forImage image: UIImage) {
guard let data = UIImageJPEGRepresentation(image, 1),
let source = CGImageSourceCreateWithData(data as CFData, nil) else { return}

if let type = CGImageSourceGetType(source) {
print("type: \(type)")
}

if let properties = CGImageSourceCopyProperties(source, nil) {
print("properties - \(properties)")
}

let count = CGImageSourceGetCount(source)
print("count: \(count)")

for index in 0..<count {
if let metaData = CGImageSourceCopyMetadataAtIndex(source, index, nil) {
print("all metaData[\(index)]: \(metaData)")

let typeId = CGImageMetadataGetTypeID()
print("metadata typeId[\(index)]: \(typeId)")


if let tags = CGImageMetadataCopyTags(metaData) as? [CGImageMetadataTag] {

print("number of tags - \(tags.count)")

for tag in tags {

let tagType = CGImageMetadataTagGetTypeID()
if let name = CGImageMetadataTagCopyName(tag) {
print("name: \(name)")
}
if let value = CGImageMetadataTagCopyValue(tag) {
print("value: \(value)")
}
if let prefix = CGImageMetadataTagCopyPrefix(tag) {
print("prefix: \(prefix)")
}
if let namespace = CGImageMetadataTagCopyNamespace(tag) {
print("namespace: \(namespace)")
}
if let qualifiers = CGImageMetadataTagCopyQualifiers(tag) {
print("qualifiers: \(qualifiers)")
}
print("-------")
}
}
}

if let properties = CGImageSourceCopyPropertiesAtIndex(source, index, nil) {
print("properties[\(index)]: \(properties)")
}
}
}

Getting metadata in swift by UIImagepickerController

Try this code:

let metadata = info[UIImagePickerControllerMediaMetadata] as? NSDictionary    
let image = info[UIImagePickerControllerOriginalImage] as? UIImage

How to get image metadata using AVFoundation?

This helper function should do the trick.

 func getEXIFFromImage(image:NSData) -> NSDictionary {

let imageSourceRef = CGImageSourceCreateWithData(image, nil);

let currentProperties = CGImageSourceCopyPropertiesAtIndex(imageSourceRef!, 0, nil)

let mutableDict = NSMutableDictionary(dictionary: currentProperties!)

return mutableDict

}

You can then use the keys from CGImageProperties to access the metadata you want.
https://developer.apple.com/reference/imageio/cgimageproperties/exif_dictionary_keys

Save and retrieve Photos with all their metadata locally on iOS using swift

Taking The image:

Take a look at the UIImagePickerController class and use it to take pictures. Taking pictures with this, will NOT save the images automatically to the iOS gallery.

The mentioned UIImagePickerController will notify a delegate UIImagePickerControllerDelegate and call the function func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]).

From the second parameter, the info dictionary, you can request most of the images metadata, livePhoto and so on. The necessary keys can be found inside the extension of UIImagePickerController called UIImagePickerController.InfoKey.

For example, here is how I then retrieve the images coordinate if available:

if let imageURL = info[UIImagePickerControllerReferenceURL] as? URL {
let result = PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil)
if let asset = result.firstObject, let location = asset.location {
let lat = location.coordinate.latitude
let lon = location.coordinate.longitude
print("Here's the lat and lon \(lat) + \(lon)")
}
}

Saving the image onto the phone within your app?:

I use JSONEncoder. You can use this class to encode and then decode your custom image class again.

Your custom image class, which has the UIImage and all the other properties must then implement the Codable protocol.
Afterwards, use the JSONEncoder to create Data of the object via its encode() method and save it to an app private location with the help of a FileManager.
Later on, you may read all files from that location again with the FileManager, decode() with the JSONEncoder and voila you have all your images again in the form of your custom image class.

Saving the image to the users gallery "Exporting":

Here's an example of how I again save that image to the users gallery as a way of exporting it:

private static func saveImageToGallery(picture: UIImage, lat: Double?, lon: Double?) {
PHPhotoLibrary.shared().performChanges({
let request = PHAssetCreationRequest.creationRequestForAsset(from: picture)
if let _lat = lat, let _lon = lon {
request.location = CLLocation(latitude: _lat, longitude: _lon)
}
})
}

I hope this will be enough to guide you to the correct way.



Related Topics



Leave a reply



Submit