读取照片的Exif信息

首先,将AssetsLibrary.framework、CoreLocation.framework和ImageIO.framework加入到项目中。

 

PhotoLibViewController.h

 

#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>

@interface PhotoLibViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
    
}

- (IBAction)toGetImage:(id)sender;

@end

 

PhotoLibViewController.m

 

#import "PhotoLibViewController.h"
#import "PhotoLibImageInfoController.h"
#import "HHLocationManager.h"
#import <ImageIO/ImageIO.h>

@implementation PhotoLibViewController

- (NSMutableDictionary *)updateExif:(CLLocation *)currentLocation{
    NSMutableDictionary *locDict = [[NSMutableDictionary alloc] init];
    CLLocationDegrees exifLatitude = currentLocation.coordinate.latitude;
    CLLocationDegrees exifLongitude = currentLocation.coordinate.longitude;
    [locDict setObject:currentLocation.timestamp forKey:(NSString *)kCGImagePropertyGPSTimeStamp];
    if (exifLatitude < 0.0) {
        exifLatitude = exifLatitude * (-1);
        [locDict setObject:@"S" forKey:(NSString *)kCGImagePropertyGPSLatitudeRef];
    } else {
        [locDict setObject:@"N" forKey:(NSString *)kCGImagePropertyGPSLatitudeRef];
    }
    [locDict setObject:[NSNumber numberWithFloat:exifLatitude] forKey:(NSString *)kCGImagePropertyGPSLatitude];
    
    if (exifLongitude < 0.0) {
        exifLongitude = exifLongitude * (-1);
        [locDict setObject:@"W" forKey:(NSString *)kCGImagePropertyGPSLongitudeRef];
    } else {
        [locDict setObject:@"E" forKey:(NSString *)kCGImagePropertyGPSLongitudeRef];
    }
    [locDict setObject:[NSNumber numberWithFloat:exifLongitude] forKey:(NSString *) kCGImagePropertyGPSLongitude];
    return [locDict autorelease]; 
}

- (void)dealloc {
    [super dealloc];
}

#pragma mark - View lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

#pragma mark - IBAction
- (IBAction)toGetImage:(id)sender {
    UIButton *button = (UIButton *)sender;
    UIImagePickerController *iconPickerController = [[UIImagePickerController alloc] init];
    if (button.tag == 51) { 
        iconPickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    } else if (button.tag == 50) { 
        iconPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        iconPickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
        iconPickerController.showsCameraControls = YES;
    }
    iconPickerController.delegate = self;
    [self presentModalViewController:iconPickerController animated:YES];
    [[HHLocationManager shared] startStandardUpdates];
    [iconPickerController release];
}

#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if (![mediaType isEqualToString:@"public.image"]) {  
        return;
    }
    NSURL *imageURL = [info objectForKey:UIImagePickerControllerReferenceURL];
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    CGImageRef imageRef = [[info objectForKey:UIImagePickerControllerOriginalImage] CGImage];
    
    typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset);
    typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);
    typedef void (^ALAssetsLibraryWriteImageCompletionBlock)(NSURL *assetURL, NSError *error);
    
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) {
        ALAssetRepresentation *rep = [myasset defaultRepresentation];
        CGImageRef iref = [rep fullResolutionImage];
        PhotoLibImageInfoController *imageInfo = [[PhotoLibImageInfoController alloc] initWithNibName:@"PhotoLibImageInfoController" bundle:nil];
        imageInfo.image = [UIImage imageWithCGImage:iref];
        imageInfo.imageInfo = [rep metadata]; 
        NSLog(@"ALAssetRepresentation: %@", [rep metadata]);
        [self.navigationController pushViewController:imageInfo animated:YES];
        [imageInfo release];
        [library release];
    };
    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror) {
        NSLog(@"Error, cant get image - %@", [myerror localizedDescription]);
        [library release];
    };
    
    ALAssetsLibraryWriteImageCompletionBlock completionblock = ^(NSURL *assetURL, NSError *error) {
        [library assetForURL:assetURL resultBlock:resultblock failureBlock:failureblock]; 
        NSLog(@"Error, cant save image - %@", [error localizedDescription]);
    };
    
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {        
        NSDictionary *gpsDict = [self updateExif:[[HHLocationManager shared] thisLocation]];
        NSMutableDictionary *imageMetaData = [NSMutableDictionary dictionary];
        [imageMetaData setObject:gpsDict forKey:(NSString *)kCGImagePropertyGPSDictionary];  
        [library writeImageToSavedPhotosAlbum:imageRef metadata:imageMetaData completionBlock:completionblock];
    } else {
        [library assetForURL:imageURL resultBlock:resultblock failureBlock:failureblock]; 
    }
    [picker performSelector:@selector(dismissModalViewControllerAnimated:) withObject:[NSNumber numberWithBool:YES] afterDelay:1.0];
}

@end

 

PhotoLibImageInfoController.h

 

#import <UIKit/UIKit.h>

@interface PhotoLibImageInfoController : UIViewController {
    
}

@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UITextView *info;
@property (nonatomic, retain) NSDictionary *imageInfo;
@property (nonatomic, retain) UIImage *image;

@end

 

PhotoLibImageInfoController.m

 

#import "PhotoLibImageInfoController.h"

@implementation PhotoLibImageInfoController
@synthesize imageView, info, imageInfo, image;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        imageInfo = [[NSDictionary alloc] init];
        image = [[UIImage alloc] init];
    }
    return self;
}

- (void)dealloc {
    [image release];
    [imageInfo release];
    [imageView release];
    [info release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (image)
        self.imageView.image = image;
    if (imageInfo) 
        self.info.text = [imageInfo description];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

@end

 

示例输出:

 

{
    ColorModel = RGB;
    DPIHeight = 100;
    DPIWidth = 100;
    Depth = 8;
    PixelHeight = 120;
    PixelWidth = 120;
    "{Exif}" = {
        ColorSpace = 1;
        ComponentsConfiguration = (
            1,
            2,
            3,
            0
        );
        ExifVersion = (
            2,
            2,
            1
        );
        FlashPixVersion = (
            1,
            0
        );
        PixelXDimension = 120;
        PixelYDimension = 120;
        SceneCaptureType = 0;
    };
    "{GPS}" = {
        Latitude = "31.2795";
	    LatitudeRef = N;
	    Longitude = "120.7431666666667";
	    LongitudeRef = E;
    };
    "{TIFF}" = {
        ResolutionUnit = 2;
        XResolution = 100;
        YResolution = 100;
        "_YCbCrPositioning" = 1;
    };
}

你可能感兴趣的:(ios,iPhone,Exif)