最近有一个需求,要求用手机拍个照片,并切需要拍摄时间,拍摄gps,拍摄具体街道信息。

首先要感谢PhotoGPSdemo的作者,你可以到这里下载demo http://www.cocoachina.com/bbs/read.php?tid=130501

以前,总认为jpg就是包含了像素信息的2进制文件,其实,jpg中还可以包含许多起它的信息,只不过我们平常用查看jpg属性时,系统没有给我们把信息全部显示出来而已!

在iOS中,提供了一组函数来操作jpg的这些额外的信息,你需要#import <ImageIO/ImageIO.h>才能使用他们。

首先,需要注意的是,UIImage对象中是没有这些信息的,它仅仅包含图像本身,jpg才包含这些信息,请不要弄混。

先看看怎么从jpg图片中取得各种额外信息,代码如下

- (IBAction)test
{
NSString * home = NSHomeDirectory();
NSData *data = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/Documents/xx.jpg",home]]; CGImageSourceRef imgSource = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); CFDictionaryRef metaDataDicRef = CGImageSourceCopyPropertiesAtIndex(imgSource,,nil);
NSLog(@"cfdictionary %@",(__bridge NSDictionary *)metaDataDicRef); NSDictionary *dic = (__bridge NSDictionary *)metaDataDicRef; NSDictionary *exifDic = [dic objectForKey:(NSString *)kCGImagePropertyExifDictionary]; NSString *location = [exifDic objectForKey:(NSString *)kCGImagePropertyExifCameraOwnerName];
NSLog(@"location is %@",location); NSString *DateTimeOriginal = [exifDic objectForKey:(NSString *)kCGImagePropertyExifDateTimeOriginal];
NSLog(@"time is %@",DateTimeOriginal); CFRelease(metaDataDicRef);
CFRelease(imgSource);
}

首先,需要用到CGImageSourceRef类,不能用UIImage类型(需要验证。。。),这种CGImageSourceRef类型可以通过函数取出jpg的额外数据,这些数据叫做metaData,翻译过来就是元数据。得到的metaData是以字典形式返回的,其中的key是固定的,具体声明见下方

CFStringRef kCGImagePropertyTIFFDictionary;
CFStringRef kCGImagePropertyGIFDictionary;
CFStringRef kCGImagePropertyJFIFDictionary;
CFStringRef kCGImagePropertyExifDictionary;
CFStringRef kCGImagePropertyPNGDictionary;
CFStringRef kCGImagePropertyIPTCDictionary;
CFStringRef kCGImagePropertyGPSDictionary;
CFStringRef kCGImagePropertyRawDictionary;
CFStringRef kCGImagePropertyCIFFDictionary;
CFStringRef kCGImageProperty8BIMDictionary;
CFStringRef kCGImagePropertyDNGDictionary;
CFStringRef kCGImagePropertyExifAuxDictionary;

这些key中,有许多key对应的对象还是一个字典,比如kCGImagePropertyExifDictionary所对应的对象包含一下key值

const CFStringRef kCGImagePropertyExifExposureTime;
const CFStringRef kCGImagePropertyExifFNumber;
const CFStringRef kCGImagePropertyExifExposureProgram;
const CFStringRef kCGImagePropertyExifSpectralSensitivity;
const CFStringRef kCGImagePropertyExifISOSpeedRatings;
const CFStringRef kCGImagePropertyExifOECF;
const CFStringRef kCGImagePropertyExifVersion;
const CFStringRef kCGImagePropertyExifDateTimeOriginal;
const CFStringRef kCGImagePropertyExifDateTimeDigitized;
const CFStringRef kCGImagePropertyExifComponentsConfiguration;
const CFStringRef kCGImagePropertyExifCompressedBitsPerPixel;
const CFStringRef kCGImagePropertyExifShutterSpeedValue;
const CFStringRef kCGImagePropertyExifApertureValue;
const CFStringRef kCGImagePropertyExifBrightnessValue;
const CFStringRef kCGImagePropertyExifExposureBiasValue;
const CFStringRef kCGImagePropertyExifMaxApertureValue;
const CFStringRef kCGImagePropertyExifSubjectDistance;
const CFStringRef kCGImagePropertyExifMeteringMode;
const CFStringRef kCGImagePropertyExifLightSource;
const CFStringRef kCGImagePropertyExifFlash;
const CFStringRef kCGImagePropertyExifFocalLength;
const CFStringRef kCGImagePropertyExifSubjectArea;
const CFStringRef kCGImagePropertyExifMakerNote;
const CFStringRef kCGImagePropertyExifUserComment;
const CFStringRef kCGImagePropertyExifSubsecTime;
const CFStringRef kCGImagePropertyExifSubsecTimeOrginal;
const CFStringRef kCGImagePropertyExifSubsecTimeDigitized;
const CFStringRef kCGImagePropertyExifFlashPixVersion;
const CFStringRef kCGImagePropertyExifColorSpace;
const CFStringRef kCGImagePropertyExifPixelXDimension;
const CFStringRef kCGImagePropertyExifPixelYDimension;
const CFStringRef kCGImagePropertyExifRelatedSoundFile;
const CFStringRef kCGImagePropertyExifFlashEnergy;
const CFStringRef kCGImagePropertyExifSpatialFrequencyResponse;
const CFStringRef kCGImagePropertyExifFocalPlaneXResolution;
const CFStringRef kCGImagePropertyExifFocalPlaneYResolution;
const CFStringRef kCGImagePropertyExifFocalPlaneResolutionUnit;
const CFStringRef kCGImagePropertyExifSubjectLocation;
const CFStringRef kCGImagePropertyExifExposureIndex;
const CFStringRef kCGImagePropertyExifSensingMethod;
const CFStringRef kCGImagePropertyExifFileSource;
const CFStringRef kCGImagePropertyExifSceneType;
const CFStringRef kCGImagePropertyExifCFAPattern;
const CFStringRef kCGImagePropertyExifCustomRendered;
const CFStringRef kCGImagePropertyExifExposureMode;
const CFStringRef kCGImagePropertyExifWhiteBalance;
const CFStringRef kCGImagePropertyExifDigitalZoomRatio;
const CFStringRef kCGImagePropertyExifFocalLenIn35mmFilm;
const CFStringRef kCGImagePropertyExifSceneCaptureType;
const CFStringRef kCGImagePropertyExifGainControl;
const CFStringRef kCGImagePropertyExifContrast;
const CFStringRef kCGImagePropertyExifSaturation;
const CFStringRef kCGImagePropertyExifSharpness;
const CFStringRef kCGImagePropertyExifDeviceSettingDescription;
const CFStringRef kCGImagePropertyExifSubjectDistRange;
const CFStringRef kCGImagePropertyExifImageUniqueID;
const CFStringRef kCGImagePropertyExifGamma;
const CFStringRef kCGImagePropertyExifCameraOwnerName;
const CFStringRef kCGImagePropertyExifBodySerialNumber;
const CFStringRef kCGImagePropertyExifLensSpecification;
const CFStringRef kCGImagePropertyExifLensMake;
const CFStringRef kCGImagePropertyExifLensModel;
const CFStringRef kCGImagePropertyExifLensSerialNumber;

这里的每一个key都有具体的含义,代表了jpg的一些信息。这些key和key的值时有严格的格式限定的,你用了自己定义的key或者不符合规则的值,是不能正确地写入jpg文件的(待会要提到如何写入metaData信息)。


如何用UIImagePickerController拍摄照片后加入信息呢?用这个控件拍照后,默认生成的metaData信息较少,只有拍摄时间等几个信息,没有gps信息,更没有街道信息,我们需要手动加入。


#pragma mark - UIImagePickerControllerDelegate



- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info


{


_mediaInfo =[NSMutableDictionarydictionaryWithDictionary:info];



if (!_locationManager) {


_locationManager = [[CLLocationManager alloc]init];


[_locationManagersetDelegate:self];


[_locationManagersetDistanceFilter:kCLDistanceFilterNone];


[_locationManagersetDesiredAccuracy:kCLLocationAccuracyBest];


}


[_locationManagerstartUpdatingLocation];



[picker dismissViewControllerAnimated:YEScompletion:^


{



}];


}



#pragma mark CLLocationManagerDelegate


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{


[manager stopUpdatingLocation];



UIImage *originalImage= (UIImage *) [_mediaInfoobjectForKey:UIImagePickerControllerOriginalImage];


NSData *imageNSData = UIImageJPEGRepresentation(originalImage,1);



CGImageSourceRef imgSource = CGImageSourceCreateWithData((__bridge CFDataRef)imageNSData, NULL);


//this is the type of image (e.g., public.jpeg)


CFStringRef UTI = CGImageSourceGetType(imgSource);



//this will be the data CGImageDestinationRef will write into


NSMutableData *newImageData = [NSMutableDatadata];


CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge  CFMutableDataRef)newImageData, UTI, 1, NULL);



if(!destination)


{


NSLog(@"***Could not create image destination ***");


return;


}



//get original metadata


NSDictionary *dict = [_mediaInfoobjectForKey:UIImagePickerControllerMediaMetadata];


NSMutableDictionary *metadata = [NSMutableDictionarydictionaryWithDictionary:dict];



//set gps info into metadata,we need a dictionary


NSDictionary * gpsDict=[newLocation GPSDictionary];


if (metadata && gpsDict) {


[metadata setValue:gpsDict forKey:(NSString*)kCGImagePropertyGPSDictionary];


}



//get location detail by CLGeocoder, this needs wifi


CLGeocoder *geocoder = [[CLGeocoder alloc] init];


[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error){


if(error != nil)


{


NSLog(@"CLGeocoder error :%@ ",error);


}


else//if we can get place info ,we set it into meteData dic with kCGImagePropertyExifCameraOwnerName key


{


if(placemarks.count > 0)


{


CLPlacemark *placemark = [placemarks objectAtIndex:0];


NSMutableDictionary *eifDic = (NSMutableDictionary *)[metadata objectForKey:(NSString *)kCGImagePropertyExifDictionary];


[eifDic setObject:placemark.nameforKey:(NSString *)kCGImagePropertyExifCameraOwnerName];


}


}



//add the image contained in the image source to the destination, overidding the old metadata with our modified metadata


CGImageDestinationAddImageFromSource(destination, imgSource, 0, (__bridge  CFDictionaryRef) metadata);



BOOL success = NO;


success = CGImageDestinationFinalize(destination);



if(!success) {


NSLog(@"***Could not create data from image destination ***");


return ;


}



CFRelease(imgSource);


CFRelease(destination);



[self sendData:newImageData];


[self writeTest:newImageData];



}];




}


 

@implementation CLLocation (GPSDictionary)

-(NSDictionary*)GPSDictionary{

NSDateFormatter *formatter  = [[NSDateFormatteralloc] init];

[formatter setDateFormat:@"hh:mm:ss.SS"];

CLLocation *location=self;

NSDictionary *gpsDict   = [NSDictionarydictionaryWithObjectsAndKeys:

[NSNumbernumberWithFloat:fabs(location.coordinate.latitude)], kCGImagePropertyGPSLatitude,

((location.coordinate.latitude >= 0) ? @"N" : @"S"), kCGImagePropertyGPSLatitudeRef,

[NSNumbernumberWithFloat:fabs(location.coordinate.longitude)], kCGImagePropertyGPSLongitude,

((location.coordinate.longitude >= 0) ? @"E" : @"W"), kCGImagePropertyGPSLongitudeRef,

[formatter stringFromDate:[location timestamp]], kCGImagePropertyGPSTimeStamp,

nil];

NSLog(@"gpsDict i %@",gpsDict);

return gpsDict;

}

@end

 

上面代码需要手动改改,它的最终目的就是用 CGImageDestinationAddImageFromSource(destination, imgSource, 0, (__bridge CFDictionaryRef) metadata);函数用新的metaData信息生成新的jpg数据。

其中gps数据用了系统自定义的key值写入,街道信息没有找到合适的key,就冒用了kCGImagePropertyExifCameraOwnerName这个key。

iOS 拍照中加入GPS和具体地理位置的更多相关文章

  1. [转]iOS开发中的火星坐标系及各种坐标系转换算法

     iOS开发中的火星坐标系及各种坐标系转换算法 源:https://my.oschina.net/u/2607703/blog/619183   其原理是这样的:保密局开发了一个系统,能将实际的坐标转 ...

  2. iOS UIWebView中javascript与Objective-C交互、获取摄像头

    UIWebView是iOS开发中常用的一个视图控件,多数情况下,它被用来显示HTML格式的内容. 支持的文档格式 除了HTML以外,UIWebView还支持iWork, Office等文档格式: Ex ...

  3. iOS开发中图片方向的获取与更改

    iOS开发中 再用到照片的时候  或多或少遇到过这样的问题  就是我想用的照片有横着拍的有竖着排的  所以导致我选取图片后的效果也横七竖八的   显示效果不好 比如: 图中红圈选中的图片选取的是横着拍 ...

  4. iOS开发中的零碎知识点笔记 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 1.关联 objc_setAssociatedObject关联是指把两个对象相互关联起来,使得其中的一个对象作为另外 ...

  5. IOS工作中的问题(转)

    1.UITableView的scrollDelegate问题 下午遇到一个奇怪的问题,之前都没有注意过,由于A VC中要实现tableView和其他View位置的联动,所以实现了tableView的d ...

  6. iOS拍照定制之AVCaptureVideoDataOutput

    问题 领导看了前面做的拍照,问了句"哪来的声音", "系统的,自带的,你看系统的拍照也有声音" "有办法能去掉吗?挺糟心的" "我 ...

  7. 总结iOS开发中的断点续传那些事儿

    前言 断点续传概述 断点续传就是从文件赏赐中断的地方重新开始下载或者上传数据,而不是从头文件开始.当下载大文件的时候,如果没有实现断点续传功能,那么每次出现异常或者用户主动的暂停,都会从头下载,这样很 ...

  8. iOS开发中静态库之".framework静态库"的制作及使用篇

    iOS开发中静态库之".framework静态库"的制作及使用篇 .framework静态库支持OC和swift .a静态库如何制作可参照上一篇: iOS开发中静态库之" ...

  9. iOS开发中静态库制作 之.a静态库制作及使用篇

    iOS开发中静态库之".a静态库"的制作及使用篇 一.库的简介 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.库的类型? 根据源代码的公开情况,库可以分为2种类 ...

随机推荐

  1. 字符串转json

    function str2json(str) { var arr = str.split('.'), json = {}, _this = json; if (!json[arr[0]]) json[ ...

  2. FastDFS在centos上的安装配置与使用

    FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题.特别适合以文件为载体的在线服务.(百 ...

  3. href的参数含有中文在IE下乱码的解决

    这是在使用kendo grid的自定义链接时遇到的一个坑,链接如下: var TempStr = "<a href='" + Url.Action("EditUse ...

  4. 【Gym 100947E】Qwerty78 Trip(组合数取模/费马小定理)

    从(1,1)到(n,m),每次向右或向下走一步,,不能经过(x,y),求走的方案数取模.可以经过(x,y)则相当于m+n步里面选n步必须向下走,方案数为 C((m−1)+(n−1),n−1) 再考虑其 ...

  5. java2集合框架的一些个人分析和理解

    Java2中的集合框架是广为人知的,本文打算从几个方面来说说自己对这个框架的理解. 下图是java.util.Collection的类图(基本完整,有些接口如集合类均实现的Cloneable.Seri ...

  6. BZOJ2186 欧拉函数

    欧拉函数:一般记作φ(n),表示1-n中与n互质的数的数量. 欧拉函数是积性函数,即φ(m*n)=φ(m)*φ(n) //这条定理基友面试时还遇到了= = 欧拉函数的值φ(n)=n*(1-p[1])* ...

  7. dijkstra,SPFA,Floyd求最短路

    Dijkstra: 裸的算法,O(n^2),使用邻接矩阵: 算法思想: 定义两个集合,一开始集合1只有一个源点,集合2有剩下的点. STEP1:在集合2中找一个到源点距离最近的顶点k:min{d[k] ...

  8. debian , ubuntu 截取下拉菜单

    普通情况下print键很好用的,但是,截下拉菜单的时候,就怎么按都没反应了...然后换了shutter也不行,只能在静态界面截图.所以...然后就有了下面方法. 如果不是gnome怎么办?这个担心显然 ...

  9. iOS开发编码建议与编程经验

    作者:乞力马扎罗的雪(GitHub) 原文 在开发过程中,我们不仅要去看别人的代码,也要让别人看我们的代码.那么,有一个良好的编码习惯将会非常重要.下面将会罗列使用Objective-C来开发iOS的 ...

  10. 弱键(Weak Key, ACM/ICPC Seoul 2004, UVa1618)

    I think: 给出k(4≤k≤5000)个互不相同的整数组成的序列Ni,判断是否存在4个整数Np.Nq.Nr和Ns(1≤p<q<r<s≤k),使得Nq>Ns>Np&g ...