Read and write iOS picture metadata

Source: Internet
Author: User
Tags color gamut

The picture itself is a variety of image data carrier, including the pixel, color, grayscale and other data information, in addition, also contains the exposure data, date, location, copyright and other metadata (metadata).

What is picture metadata

Metadata includes a number of important information, commonly known as EXIF (exchangeable image File format, exchangeable images, Tagged), GPS (location information), and TIFF (image File format).

The so-called meta-data refers to data about data, which is used to describe the information.

Take EXIF for example, which contains a lot of information:

image description  image description, source .  refers to the tool that generates the image artist  author   Some cameras can enter the name of the user make  producer   refers to the product manufacturer model  Model   refers to the device model orientation direction   Some camera support, some do not support xresolution/yresolution x/y direction resolution   There are specific articles in this section explaining this issue. Resolutionunit resolution units   General Ppisoftware software   display firmware firmware version datetime date and time ycbcrpositioning  The hue locates the Exifoffsetexif information location, defines the Exif's write in the file, and some software does not display. exposuretime  exposure time   The shutter speed fnumber aperture factor exposureprogram Exposure program   refers to the setting of program AE, different camera, may be sutter  Priority (shutter first), aperture priority (shutter priority), etc. iso speed  Ratings sensitivity EXIFVERSIONEXIF version datetimeoriginal creation time datetimedigitized digital time componentsconfiguration image construction (multi-finger color combination scheme) CompressedBitsP Erpixel (BPP) compression exposurebiasvalue exposure compensation per pixel color bits   means compression level. Maxaperturevalue maximum aperture Meteringmode metering method,  average metering, center-weighted metering, spot metering, etc. LightSource Light source   white balance set whether Flash uses Flash. Focallength focal length, generally showing the lens physical focal length, some software can define a coefficient, thus showing the equivalent of 35mm camera focal length  makernote (user comment) Author's mark, description, Record Flashpixversionflashpix version   (individual models supported) ColorSpace color gamut, color space exifimagewidth (pixel x dimension) Image width   Number of horizontal pixels exifimagelength (pixel y dimension) Image height   refers to the number of portrait pixels interoperability  IFD commonality Extension definition Pointers   TIFF file related, meaning unknown filesource source file  compression compression ratio.

This data is the element that makes up a picture, also is the same as a picture business card, contains the rich information.

You can find out more about EXIF in this study.

Use in development

Most of the smartphones on the market currently take photos that support writing metadata, so we can use the metadata of the images to carry the data that we need to add. In processing pictures, we often need to manipulate the image data, in addition to manipulating the image information, we can also manipulate the image information metadata, including deposit and fetch. This is very convenient for passing data, where one party simply writes the data to the picture, and the other side can get the data to be passed by reading the picture's metadata, without having to pass any objects outside the picture, and there are many scenarios in the actual development.

My Codes
    • Metadata Read

    • First, we need to use UIImagePickerControllerDelegate the proxy method to create one UIImagePickerController , which is available in its proxy method, to - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info get the info image object:

UIImage *image = [info valueforkey:uiimagepickercontrolleroriginalimage];
    1. to get info metadata , to use alassetslibrary of Assetforurl:resultblock: method:

 //1.  First Get reference url    nsurl *asseturl = [info  objectforkey:uiimagepickercontrollerreferenceurl];    //2.  Create a alassetslibrary     ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];     //3.  use the callback method to get the metadata    [library assetforurl:asseturl of the dictionary type              resultblock:^ (ALAsset * Asset)  {                  nsdictionary *imagemetadata = [[nsmutabledictionary alloc] initwithdictionary: asset.defaultrepresentation.metadata];                  nslog (@ "metadata:--%@", Imagemetadata);                  nsdictionary *gpsdic = [imagemetadata objectforkey:@ "{ GPS} "];                  nsdictionary *exifdic = [imagemetadata objectforkey:@ "{Exif}"];                  NSDictionary *tiffDic  = [imagemetadata objectforkey:@ "{TIFF}"];                  //Interchangeable image Files                   nslog (@ "exif info:--%@", ExifDic);                  //Location Info                   nslog (@ "GPS info:--%@ ", gpsdic);                  //image File Format                   nslog (@ "tiff  info:--%@ ", tiffdic);              }
    • Metadata Write

Take an example of kcgimagepropertyexifdatetimeoriginal that writes EXIF to the picture metadata.

1. First create a Date object in a specific format:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [Formatter setdateformat:@ "YYYY:MM:dd Hh:mm:ss"]; NSString *now = [formatter stringfromdate:[nsdate date]];

2. exifDic set the corresponding field value in the previous to the current date object now :

[Exifdic setvalue:now Forkey: (nsstring*) kcgimagepropertyexifdatetimeoriginal]; [Imagemetadata setvalue:exifdic forkey:@ "{Exif}"];

3. Write and save using Alassetslibrary's WriteImageToSavedPhotosAlbum:metadata:completionBlock method:

[library writeimagetosavedphotosalbum:[image cgimage] metadata: imagemetadata completionblock:^ (Nsurl *asseturl, nserror *error)  {                      if   (Error == nil)                           nslog (@ "metadata write  Success! ");                       else                          nslog (@ "write error:%@", Error.userInfo) ;                  }]; 

In this way, the creation time of the picture is written and saved, and if this is done for a picture that already exists in the album, it will be saved by the time the change was created.

    • Extended

If you want to write in Chinese or custom data, consider writing data to kcgimagepropertyexifusercomment.

Usercomment Label Description
Tag number: 0x9286; format: undefined; Description: Stores the user's comment, which allows the use of a two-byte German character or Unicode, the first 8 bytes describe the character set, ' JIS ' is Japanese (famous for Kanji).
' 0x41,0x53,0x43,0x49,0x49,0x00,0x00,0x00 ': ASCII
' 0x4a,0x49,0x53,0x00,0x00,0x00,0x00,0x00 ': JIS
' 0x55,0x4e,0x49,0x43,0x4f,0x44,0x45,0x00 ': Unicode
' 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ': Undefined

Usercomment Write code implementation

NSString *usercomment = @ "Hello, this was a test text for writing data in Usercomment";                 [Exifdic setvalue:usercomment Forkey: (nsstring*) kcgimagepropertyexifusercomment]; [Imagemetadata setvalue:exifdic forkey:@ "{Exif}"];

To facilitate understanding and extension, the author submits a deme:imagemetadata on GitHub.

Read and write iOS picture metadata

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.