1. The introduction of Exif

The Exchangeable image file format is often referred to as Exif (Exchangeable Image File Format), which is specially set for digital camera photos and can record the attribute information and shooting data of digital photos.

Exif can be attached to JPEG, TIFF, RIFF, Exif, GPS and other files to increase the content of the digital camera shooting information and the version information of the index map or image processing software.

The Exif message starts with 0xFFE1, and the last two bytes indicate the length of the Exif message. So Exif information is up to 64 kB in size and TIFF format internally.

2. Read Exif information

Exif information is realized through the ImageIO framework, which is in the lower level of iOS, so the interface provided is C-style, and the key data is also stored by CoreFoundation. Data manipulation requires bridging transformations between CoreFoundation and superfoundation.

  1. Get image files
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"YourPic" withExtension:@""];
Copy the code

2. Create CGImageSourceRef

CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)fileUrl, NULL);
Copy the code

3. Use imageSource to retrieve all ExifData

CFDictionaryRef imageInfo = CGImageSourceCopyPropertiesAtIndex(imageSource, 0,NULL);
Copy the code

4. Extract the EXIF file from all ExifData

NSDictionary *exifDic = (__bridge NSDictionary *)CFDictionaryGetValue(imageInfo, kCGImagePropertyExifDictionary) ;
Copy the code

5. Print all Exif information and Exif file information

NSLog(@"All Exif Info:%@",imageInfo);
NSLog(@"EXIF:%@",exifDic);
Copy the code

3. Write Exif information

  1. Get the EXIF file and GPS file in the picture
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"YourImage"], 1);

CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);

NSDictionary *imageInfo = (__bridge NSDictionary*)CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);

NSMutableDictionary *metaDataDic = [imageInfo mutableCopy];
NSMutableDictionary *exifDic =[[metaDataDic objectForKey:(NSString*)kCGImagePropertyExifDictionary]mutableCopy];
NSMutableDictionary *GPSDic =[[metaDataDic objectForKey:(NSString*)kCGImagePropertyGPSDictionary]mutableCopy];
Copy the code
  1. Modify some information in EXIF files and GPS files
1234.3] [exifDic setObject: [NSNumber numberWithFloat: forKey: (kCGImagePropertyExifExposureTime nsstrings *)]; [exifDic setObject:@"SenseTime" forKey:(NSString *)kCGImagePropertyExifLensModel]; [GPSDic setObject:@"N" forKey:(NSString*)kCGImagePropertyGPSLatitudeRef]; 116.29353] [GPSDic setObject: [NSNumber numberWithFloat: forKey: (kCGImagePropertyGPSLatitude nsstrings *)]; [metaDataDic setObject:exifDic forKey:(NSString*)kCGImagePropertyExifDictionary]; [metaDataDic setObject:GPSDic forKey:(NSString*)kCGImagePropertyGPSDictionary];Copy the code
  1. Write the modified file to the image
CFStringRef UTI = CGImageSourceGetType(source); NSMutableData *newImageData = [NSMutableData data]; CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)newImageData, UTI, 1,NULL); //add the image contained in the image source to the destination, overidding the old metadata with our modified metadata CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef)metaDataDic); CGImageDestinationFinalize (destination);Copy the code
  1. Save the picture
NSString *directoryDocuments =  NSTemporaryDirectory();
[newImageData writeToFile: directoryDocuments atomically:YES];
Copy the code
  1. View the Exif information of the modified picture
CIImage *testImage = [CIImage imageWithData:newImageData];
NSDictionary *propDict = [testImage properties];
NSLog(@"Properties %@", propDict);    
Copy the code