Developers frequently want to know how to extract metadata from an image file on iOS using MonoTouch. One alternative is to use MonoTouch.ImageIO.CGImageSource
to query the image (without even having to load it into memory).
Here is an example on gist that shows how to use the CGImageSource
class, and a screenshot of how it looks (you'll have to add your own image to the project):
The key lines of code are:
var imageFilename = "img.jpg"; var url = new NSUrl(imageFilename, false); CGImageSource myImageSource; myImageSource = CGImageSource.FromUrl (url, null); var ns = new NSDictionary(); var imageProperties = myImageSource.CopyProperties(ns, 0); // Output ALL teh things //Console.WriteLine(imageProperties.DescriptionInStringsFileFormat); // Basic Properties var width = imageProperties[CGImageProperties.PixelWidth]; var height = imageProperties[CGImageProperties.PixelHeight]; var orientation = imageProperties[CGImageProperties.Orientation]; var dimensions = String.Format ("Dimensions: {0}x{1} (orientation {2})", width, height, orientation); Console.WriteLine(dimensions); // TIFF Properties var tiff = imageProperties.ObjectForKey(CGImageProperties.TIFFDictionary) as NSDictionary; var make = tiff[CGImageProperties.TIFFMake]; var model = tiff[CGImageProperties.TIFFModel]; var dt = tiff[CGImageProperties.TIFFDateTime]; var tprops = String.Format ("TIFF: {0} {1} {2}", make, model, dt); Console.WriteLine(tprops); // GPS Properties var gps = imageProperties.ObjectForKey(CGImageProperties.GPSDictionary) as NSDictionary; var lat = gps[CGImageProperties.GPSLatitude]; var latref = gps[CGImageProperties.GPSLatitudeRef]; var lon = gps[CGImageProperties.GPSLongitude]; var lonref = gps[CGImageProperties.GPSLongitudeRef]; var loc = String.Format ("GPS: {0} {1}, {2} {3}", lat, latref, lon, lonref); Console.WriteLine(loc); // EXIF Properties var exif = imageProperties.ObjectForKey(CGImageProperties.ExifDictionary) as NSDictionary; var fn = exif[CGImageProperties.ExifFNumber]; var focal = exif[CGImageProperties.ExifFocalLength]; var eprops = String.Format ("EXIF: Fstop {0} FocalLength {1}", fn, focal); Console.WriteLine(eprops);Xamarin already has a recipe explaining how to save a photo with metadata.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.