Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Access image metadata with MonoTouch (eg. GPS, EXIF data)
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace AccessImageMetadata
{
public class Application
{
// This is the main entry point of the application.
static void Main (string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main (args, null, "AppDelegate");
}
}
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
// class-level declarations
UIWindow window;
UINavigationController navigationController;
UIViewController imageViewController;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
// create a new window instance based on the screen size
window = new UIWindow (UIScreen.MainScreen.Bounds);
navigationController = new UINavigationController ();
imageViewController = new ImageViewController ();
navigationController.PushViewController(imageViewController, false);
window.RootViewController = navigationController;
// make the window visible
window.MakeKeyAndVisible ();
return true;
}
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.CoreFoundation;
using MonoTouch.ImageIO;
// Image Properties reference
// http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGImageProperties_Reference/Reference/reference.html#//apple_ref/doc/uid/TP40005103
namespace AccessImageMetadata
{
public class ImageViewController : UIViewController
{
// UI controls
UIImageView imageView;
UITextView output;
UILabel overlabel;
public ImageViewController ()
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
View.BackgroundColor = UIColor.White;
Title = "Image Metadata";
var imageFilename = "img.jpg"; // ADD YOUR IMAGE HERE
// original sample
// http://developer.apple.com/library/ios/ipad/#qa/qa1654/_index.html
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);
// http://stackoverflow.com/questions/4043685/problem-in-writing-metadata-to-image
// 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);
// Now create UI controls to display this stuff
imageView = new UIImageView(new RectangleF(0,0,320,240));
output = new UITextView(new RectangleF(0,235,320,180));
overlabel = new UILabel(new RectangleF(0, 0, 220, 60));
imageView.Image = UIImage.FromFile(imageFilename);
output.Text = imageProperties.DescriptionInStringsFileFormat;
output.Editable = false;
output.ScrollEnabled = true;
overlabel.TextColor = UIColor.White;
overlabel.BackgroundColor = UIColor.Clear;
overlabel.Font = UIFont.SystemFontOfSize(10f);
overlabel.Lines = 4;
overlabel.Text = dimensions + "\n" + loc + "\n" + tprops + "\n" + eprops;
View.AddSubview (imageView);
View.AddSubview (output);
View.AddSubview (overlabel);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment