获取图片的metaData
获取简易的metaData较为容易,以下是测试图:
以下是本人提供的源码:
UIImage+MetaData.h
// // UIImage+MetaData.h // PictureInfo // // Created by YouXianMing on 14-8-27. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import <UIKit/UIKit.h> @interface UIImage (MetaData) - (NSDictionary *)JPEGmetaData; - (NSDictionary *)PNGmetaData; @end
UIImage+MetaData.h
// // UIImage+MetaData.m // PictureInfo // // Created by YouXianMing on 14-8-27. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import "UIImage+MetaData.h" #import <AssetsLibrary/AssetsLibrary.h> #import <ImageIO/ImageIO.h> @implementation UIImage (MetaData) - (NSDictionary *)JPEGmetaData { if (self == nil) { return nil; } // 转换成jpegData,信息要多一些 NSData *jpegData = UIImageJPEGRepresentation(self, 1.0); CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)jpegData, NULL); CFDictionaryRef imageMetaData = CGImageSourceCopyPropertiesAtIndex(source, 0, NULL); CFRelease(source); NSDictionary *metaDataInfo = CFBridgingRelease(imageMetaData); return metaDataInfo; } - (NSDictionary *)PNGmetaData { if (self == nil) { return nil; } NSData *pngData = UIImagePNGRepresentation(self); CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)pngData , NULL); CFDictionaryRef imageMetaData = CGImageSourceCopyPropertiesAtIndex(source, 0, NULL); CFRelease(source); NSDictionary *metaDataInfo = CFBridgingRelease(imageMetaData); return metaDataInfo; } @end
使用情况:
// // AppDelegate.m // GetPictureInfo // // Copyright (c) 2014年 Y.X. All rights reserved. // #import "AppDelegate.h" #import "UIImage+MetaData.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. NSLog(@"%@", [[UIImage imageNamed:@"IMG_0151.JPG"] JPEGmetaData]); self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; } @end
以下是打印信息:
{
ColorModel = RGB;
Depth = 8;
Orientation = 1;
PixelHeight = 1936;
PixelWidth = 2592;
"{Exif}" = {
ColorSpace = 1;
PixelXDimension = 2592;
PixelYDimension = 1936;
};
"{JFIF}" = {
DensityUnit = 0;
JFIFVersion = (
1,
1
);
XDensity = 1;
YDensity = 1;
};
"{TIFF}" = {
Orientation = 1;
};
}
几个需要注意的地方:
1. 需要引入两个库
2. 一些需要注意的细节
你以为结束了么?没有呢,你还没取到图片的经纬度信息,对吧,一下给你提供资料自己去尝试:)
http://stackoverflow.com/questions/9766394/get-exif-data-from-uiimage-uiimagepickercontroller
add a comment
|
Interesting question! I came up with the following solution working for images picked from your photo library (note my code is using ARC): Import Then include the needed classes inside your .h-file:
And put this inside your
One thing I noticed is, that iOS will ask the user to allow location services – if he denies, you won't be abled to get the image data … EDIT Added code to save the image including its meta data. It's a quick approach, so maybe there is a better way, but it works! |
||||
I had found solution and got answer from here From here We can get GPS info as well.. Amazing and thanks all for helping me to solve this problem. UPDATE This is another function that I had created myself, also return Exif
data as well as GPS data and in this function we doesn't need any third
party library.. but you have to turn on location services for this. and
use current latitude and longitude for that. so have to use
and We will retrieve that data like this
For all of this you should have to Import |
|||||||||||||||||||||
|
These answers all seem extremely complex. If the image has been saved to the Camera Roll, and you have the ALAsset (either from UIImagePicker or ALAssetLibrary) you can get the metadata like so:
If you want to save that image from camera roll to another location (say in Sandbox/Documents) simply do:
|
|||||||||
|
You need ALAssetsLibrary to actually retrieve the EXIF info from an image. The EXIF is added to an image only when it is saved to the Photo Library. Even if you use ALAssetLibrary to get an image asset from the library, it will lose all EXIF info if you set it to a UIImage. |
|||
I have tried to insert GPS coordinates into image metadata picked by iPad Camera as it was suggested by Mehul. It Works, Thank you for your post. P.S. Who intends to use that code, just substitude the two geolocations at the top of the function -(NSMutableData *)getImageWithMetaData:(UIImage *)pImage {
... By supposing that you have already initializied somewhere locationManager in your code, like this:
and by importing CoreLocation/CoreLocation.h and ImageIO/ImageIO.h headers with associated frameworks. |
__bridge
is one of a hand full of keywords, which tell ARC about the objects ownership so it can properly clean them up. The simplest case is a__bridge
cast, for which ARC will not do any extra work (it assumes you handle the object's memory yourself). – dom Mar 21 '12 at 6:45