从UIImage获取Exif数据

从UIImage获取Exif数据

本文介绍了从UIImage获取Exif数据 - UIImagePickerController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何从UIImagePickerController中选择UIImage获取Exif信息?



我为此做了很多R& D并获得了很多回复,但仍未能实现这一点。



我已经浏览了这个和链接





提前致谢..

解决方案

我找到了解决方案,并从



从这里我们也可以获得GPS信息..



非常感谢所有帮助我解决这个问题。



UPDATE



这是我的另一个功能CRE我自己,也返回Exif数据以及GPS数据,在此功能中我们不需要任何第三方库..但你必须为此打开位置服务。并使用当前的纬度和经度。所以必须使用 CoreLocation.framework

  //对于CAMERA IMAGE 

- (NSMutableData *)getImageWithMetaData:(UIImage *)pImage
{
NSData * pngData = UIImagePNGRepresentation(pImage);

CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)pngData,NULL);
NSDictionary * metadata =(NSDictionary *)CGImageSourceCopyPropertiesAtIndex(source,0,NULL);

NSMutableDictionary * metadataAsMutable = [[metadata mutableCopy] autorelease];
[元数据发布];

//对于GPS词典
NSMutableDictionary * GPSDictionary = [[[metadataAsMutable objectForKey:(NSString *)kCGImagePropertyGPSDictionary] mutableCopy] autorelease];
if(!GPSDictionary)
GPSDictionary = [NSMutableDictionary dictionary];

[GPSDictionary setValue:[NSNumber numberWithDouble:currentLatitude] forKey:(NSString *)kCGImagePropertyGPSLatitude];
[GPSDictionary setValue:[NSNumber numberWithDouble:currentLongitude] forKey:(NSString *)kCGImagePropertyGPSLongitude];

NSString * ref;
if(currentLatitude< 0.0)
ref = @S;
else
ref = @N;
[GPSDictionary setValue:ref forKey:(NSString *)kCGImagePropertyGPSLatitudeRef];

if(currentLongitude< 0.0)
ref = @W;
else
ref = @E;
[GPSDictionary setValue:ref forKey:(NSString *)kCGImagePropertyGPSLongitudeRef];

[GPSDictionary setValue:[NSNumber numberWithFloat:location.altitude] forKey:(NSString *)kCGImagePropertyGPSAltitude];

//对于EXIF字典
NSMutableDictionary * EXIFDictionary = [[[metadataAsMutable objectForKey:(NSString *)kCGImagePropertyExifDictionary] mutableCopy] autorelease];
if(!EXIFDictionary)
EXIFDictionary = [NSMutableDictionary dictionary];

[EXIFDictionary setObject:[NSDate date] forKey:(NSString *)kCGImagePropertyExifDateTimeOriginal];
[EXIFDictionary setObject:[NSDate date] forKey:(NSString *)kCGImagePropertyExifDateTimeDigitized];

//将修改后的EXIF数据添加回图像的元数据
[metadataAsMutable setObject:EXIFDictionary forKey:(NSString *)kCGImagePropertyExifDictionary];
[metadataAsMutable setObject:GPSDictionary forKey:(NSString *)kCGImagePropertyGPSDictionary];

CFStringRef UTI = CGImageSourceGetType(source);

NSMutableData * dest_data = [NSMutableData data];
CGImageDestinationRef destination = CGImageDestinationCreateWithData((CFMutableDataRef)dest_data,UTI,1,NULL);

if(!destination)
dest_data = [[pngData mutableCopy] autorelease];
else
{
CGImageDestinationAddImageFromSource(destination,source,0,(CFDictionaryRef)metadataAsMutable);
BOOL success = CGImageDestinationFinalize(destination);
if(!success)
dest_data = [[pngData mutableCopy] autorelease];
}

if(destination)
CFRelease(destination);

CFRelease(来源);

返回dest_data;
}

// FOR PHOTO LIBRARY IMAGE

- (NSMutableData *)getImagedataPhotoLibrary:(NSDictionary *)pImgDictionary andImage:(UIImage *)pImage
{
NSData * data = UIImagePNGRepresentation(pImage);

CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)data,NULL);
NSMutableDictionary * metadataAsMutable = [[pImgDictionary mutableCopy] autorelease];

CFStringRef UTI = CGImageSourceGetType(source);

NSMutableData * dest_data = [NSMutableData data];

//对于Mutabledata
CGImageDestinationRef destination = CGImageDestinationCreateWithData((CFMutableDataRef)dest_data,UTI,1,NULL);

if(!destination)
dest_data = [[data mutableCopy] autorelease];
else
{
CGImageDestinationAddImageFromSource(destination,source,0,(CFDictionaryRef)metadataAsMutable);
BOOL success = CGImageDestinationFinalize(destination);
if(!success)
dest_data = [[data mutableCopy] autorelease];
}
if(destination)
CFRelease(destination);

CFRelease(来源);

返回dest_data;
}

我们将检索这样的数据

  // FOR CAMERA IMAGE 
NSData * originalImgData = [self getImageWithMetaData:imgOriginal];

// FOR PHOTO LIBRARY IMAGE
[self getImagedataPhotoLibrary:[[myasset defaultRepresentation] metadata] andImage:imgOriginal];

对于所有这些,您必须导入 AssetsLibrary.framework ImageIO.framework


How can we get Exif information from UIImage selected from UIImagePickerController?

I had done much R&D for this and got many replies but still failed to implement this.

I had gone through this this and this link

Please help me to solve this problem.

Thanks in advance..

解决方案

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 CoreLocation.framework

//FOR CAMERA IMAGE

-(NSMutableData *)getImageWithMetaData:(UIImage *)pImage
{
    NSData* pngData =  UIImagePNGRepresentation(pImage);

    CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)pngData, NULL);
    NSDictionary *metadata = (NSDictionary *) CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);

    NSMutableDictionary *metadataAsMutable = [[metadata mutableCopy]autorelease];
    [metadata release];

    //For GPS Dictionary
    NSMutableDictionary *GPSDictionary = [[[metadataAsMutable objectForKey:(NSString *)kCGImagePropertyGPSDictionary]mutableCopy]autorelease];
    if(!GPSDictionary)
        GPSDictionary = [NSMutableDictionary dictionary];

    [GPSDictionary setValue:[NSNumber numberWithDouble:currentLatitude] forKey:(NSString*)kCGImagePropertyGPSLatitude];
    [GPSDictionary setValue:[NSNumber numberWithDouble:currentLongitude] forKey:(NSString*)kCGImagePropertyGPSLongitude];

    NSString* ref;
    if (currentLatitude <0.0)
        ref = @"S";
    else
        ref =@"N";
    [GPSDictionary setValue:ref forKey:(NSString*)kCGImagePropertyGPSLatitudeRef];

    if (currentLongitude <0.0)
        ref = @"W";
    else
        ref =@"E";
    [GPSDictionary setValue:ref forKey:(NSString*)kCGImagePropertyGPSLongitudeRef];

    [GPSDictionary setValue:[NSNumber numberWithFloat:location.altitude] forKey:(NSString*)kCGImagePropertyGPSAltitude];

    //For EXIF Dictionary
    NSMutableDictionary *EXIFDictionary = [[[metadataAsMutable objectForKey:(NSString *)kCGImagePropertyExifDictionary]mutableCopy]autorelease];
    if(!EXIFDictionary)
        EXIFDictionary = [NSMutableDictionary dictionary];

    [EXIFDictionary setObject:[NSDate date] forKey:(NSString*)kCGImagePropertyExifDateTimeOriginal];
    [EXIFDictionary setObject:[NSDate date] forKey:(NSString*)kCGImagePropertyExifDateTimeDigitized];

    //add our modified EXIF data back into the image’s metadata
    [metadataAsMutable setObject:EXIFDictionary forKey:(NSString *)kCGImagePropertyExifDictionary];
    [metadataAsMutable setObject:GPSDictionary forKey:(NSString *)kCGImagePropertyGPSDictionary];

    CFStringRef UTI = CGImageSourceGetType(source);

    NSMutableData *dest_data = [NSMutableData data];
    CGImageDestinationRef destination = CGImageDestinationCreateWithData((CFMutableDataRef)dest_data, UTI, 1, NULL);

    if(!destination)
        dest_data = [[pngData mutableCopy] autorelease];
    else
    {
        CGImageDestinationAddImageFromSource(destination, source, 0, (CFDictionaryRef) metadataAsMutable);
        BOOL success = CGImageDestinationFinalize(destination);
        if(!success)
            dest_data = [[pngData mutableCopy] autorelease];
    }

    if(destination)
        CFRelease(destination);

    CFRelease(source);

    return dest_data;
}

//FOR PHOTO LIBRARY IMAGE

-(NSMutableData *)getImagedataPhotoLibrary:(NSDictionary *)pImgDictionary andImage:(UIImage *)pImage
{
    NSData* data = UIImagePNGRepresentation(pImage);

    CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)data, NULL);
    NSMutableDictionary *metadataAsMutable = [[pImgDictionary mutableCopy]autorelease];

    CFStringRef UTI = CGImageSourceGetType(source);

    NSMutableData *dest_data = [NSMutableData data];

    //For Mutabledata
    CGImageDestinationRef destination = CGImageDestinationCreateWithData((CFMutableDataRef)dest_data, UTI, 1, NULL);

    if(!destination)
        dest_data = [[data mutableCopy] autorelease];
    else
    {
        CGImageDestinationAddImageFromSource(destination, source, 0, (CFDictionaryRef) metadataAsMutable);
        BOOL success = CGImageDestinationFinalize(destination);
        if(!success)
            dest_data = [[data mutableCopy] autorelease];
    }
    if(destination)
        CFRelease(destination);

    CFRelease(source);

    return dest_data;
}

and We will retrieve that data like this

//FOR CAMERA IMAGE
NSData *originalImgData = [self getImageWithMetaData:imgOriginal];

//FOR PHOTO LIBRARY IMAGE
[self getImagedataPhotoLibrary:[[myasset defaultRepresentation] metadata] andImage:imgOriginal];

For all of this you should have to Import AssetsLibrary.framework and ImageIO.framework.

这篇关于从UIImage获取Exif数据 - UIImagePickerController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:45