You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by lo...@apache.org on 2013/05/10 16:38:58 UTC
[2/2] ios commit: [CB-2905] Exif geolocation meta data tag writing
[CB-2905] Exif geolocation meta data tag writing
UIImagePickerController does not return geo data, CDVCamera is now a CLLocationManagerDelegate
when image is taken, CDVCamera begins gps update, waits for success/fail
if success, writes geo data to dicitonary which is passed to tag writing
if fail, meta data written as normal
Project: http://git-wip-us.apache.org/repos/asf/cordova-ios/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-ios/commit/f01dad2b
Tree: http://git-wip-us.apache.org/repos/asf/cordova-ios/tree/f01dad2b
Diff: http://git-wip-us.apache.org/repos/asf/cordova-ios/diff/f01dad2b
Branch: refs/heads/master
Commit: f01dad2b00dbf05dbca32729f6005d52e61f96de
Parents: fe5f542
Author: lorinbeer <lo...@adobe.com>
Authored: Fri May 10 06:19:36 2013 -0700
Committer: lorinbeer <lo...@adobe.com>
Committed: Fri May 10 07:27:24 2013 -0700
----------------------------------------------------------------------
CordovaLib/Classes/CDVCamera.h | 7 ++-
CordovaLib/Classes/CDVCamera.m | 94 +++++++++++++++++++++++++----------
2 files changed, 74 insertions(+), 27 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/f01dad2b/CordovaLib/Classes/CDVCamera.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVCamera.h b/CordovaLib/Classes/CDVCamera.h
index 3e77ed3..2932e3b 100644
--- a/CordovaLib/Classes/CDVCamera.h
+++ b/CordovaLib/Classes/CDVCamera.h
@@ -18,6 +18,7 @@
*/
#import <Foundation/Foundation.h>
+#import <CoreLocation/CoreLocation.h>
#import <CoreLocation/CLLocationManager.h>
#import "CDVPlugin.h"
@@ -63,12 +64,14 @@ typedef NSUInteger CDVMediaType;
@interface CDVCamera : CDVPlugin <UIImagePickerControllerDelegate,
UINavigationControllerDelegate,
- UIPopoverControllerDelegate>
+ UIPopoverControllerDelegate,
+ CLLocationManagerDelegate>
{}
@property (strong) CDVCameraPicker* pickerController;
@property (strong) NSMutableDictionary *metadata;
@property (strong, nonatomic) CLLocationManager *locationManager;
+@property (strong) NSData* data;
/*
* getPicture
@@ -92,6 +95,8 @@ typedef NSUInteger CDVMediaType;
- (UIImage*)imageByScalingAndCroppingForSize:(UIImage*)anImage toSize:(CGSize)targetSize;
- (UIImage*)imageByScalingNotCroppingForSize:(UIImage*)anImage toSize:(CGSize)frameSize;
- (UIImage*)imageCorrectedForCaptureOrientation:(UIImage*)anImage;
+
+- (void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation;
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error;
@end
http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/f01dad2b/CordovaLib/Classes/CDVCamera.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVCamera.m b/CordovaLib/Classes/CDVCamera.m
index 1b8d676..1ee641c 100644
--- a/CordovaLib/Classes/CDVCamera.m
+++ b/CordovaLib/Classes/CDVCamera.m
@@ -307,34 +307,18 @@ static NSSet* org_apache_cordova_validArrowDirections;
if (cameraPicker.encodingType == EncodingTypePNG) {
data = UIImagePNGRepresentation(scaledImage == nil ? image : scaledImage);
} else {
- data = UIImageJPEGRepresentation(scaledImage == nil ? image : scaledImage, cameraPicker.quality / 100.0f);
-
- /* splice loc */
- CDVJpegHeaderWriter* exifWriter = [[CDVJpegHeaderWriter alloc] init];
- NSString* headerstring = [exifWriter createExifAPP1:[info objectForKey:@"UIImagePickerControllerMediaMetadata"]];
- // data = [exifWriter spliceExifBlockIntoJpeg:data withExifBlock:headerstring];
-
- //NSMutableDictionary *metadata;
- [[self locationManager] startUpdatingLocation];
-
+ self.data = UIImageJPEGRepresentation(scaledImage == nil ? image : scaledImage, cameraPicker.quality / 100.0f);
+
NSDictionary *controllerMetadata = [info objectForKey:@"UIImagePickerControllerMediaMetadata"];
if (controllerMetadata) {
self.metadata = [[NSMutableDictionary alloc] init];
- NSMutableDictionary *EXIFDictionary = [[controllerMetadata objectForKey:(NSString *)kCGImagePropertyExifDictionary]mutableCopy];
- if (EXIFDictionary) {
- [self.metadata setObject:EXIFDictionary forKey:(NSString *)kCGImagePropertyExifDictionary];
- }
+
+ NSMutableDictionary *EXIFDictionary = [[controllerMetadata objectForKey:(NSString *)kCGImagePropertyExifDictionary]mutableCopy];
+ if (EXIFDictionary) [self.metadata setObject:EXIFDictionary forKey:(NSString *)kCGImagePropertyExifDictionary];
+
+ [[self locationManager] startUpdatingLocation];
+ return;
}
-
- CGImageSourceRef sourceImage = CGImageSourceCreateWithData((__bridge_retained CFDataRef)data, NULL);
- CFStringRef sourceType = CGImageSourceGetType(sourceImage);
-
- CGImageDestinationRef destinationImage = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)data, sourceType, 1, NULL);
- CGImageDestinationAddImageFromSource(destinationImage, sourceImage, 0, (__bridge CFDictionaryRef)self.metadata);
- CGImageDestinationFinalize(destinationImage);
-
- CFRelease(sourceImage);
- CFRelease(destinationImage);
}
if (cameraPicker.saveToPhotoAlbum) {
@@ -654,7 +638,7 @@ static NSSet* org_apache_cordova_validArrowDirections;
[GPSDictionary setObject:[formatter stringFromDate:newLocation.timestamp] forKey:(NSString *)kCGImagePropertyGPSDateStamp];
[self.metadata setObject:GPSDictionary forKey:(NSString *)kCGImagePropertyGPSDictionary];
- //[self imagePickerControllerReturnImageResult];
+ [self imagePickerControllerReturnImageResult];
}
}
@@ -663,10 +647,68 @@ static NSSet* org_apache_cordova_validArrowDirections;
[self.locationManager stopUpdatingLocation];
self.locationManager = nil;
- //[self imagePickerControllerReturnImageResult];
+ [self imagePickerControllerReturnImageResult];
}
}
+- (void)imagePickerControllerReturnImageResult
+{
+ CDVPluginResult* result = nil;
+
+ if (self.metadata) {
+ CGImageSourceRef sourceImage = CGImageSourceCreateWithData((__bridge_retained CFDataRef)self.data, NULL);
+ CFStringRef sourceType = CGImageSourceGetType(sourceImage);
+
+ CGImageDestinationRef destinationImage = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)self.data, sourceType, 1, NULL);
+ CGImageDestinationAddImageFromSource(destinationImage, sourceImage, 0, (__bridge CFDictionaryRef)self.metadata);
+ CGImageDestinationFinalize(destinationImage);
+
+ CFRelease(sourceImage);
+ CFRelease(destinationImage);
+ }
+
+ if (self.pickerController.saveToPhotoAlbum) {
+ UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:[self data]], nil, nil, nil);
+ }
+
+ if (self.pickerController.returnType == DestinationTypeFileUri) {
+ // write to temp directory and return URI
+ // get the temp directory path
+ NSString* docsPath = [NSTemporaryDirectory()stringByStandardizingPath];
+ NSError* err = nil;
+ NSFileManager* fileMgr = [[NSFileManager alloc] init]; // recommended by apple (vs [NSFileManager defaultManager]) to be threadsafe
+ // generate unique file name
+ NSString* filePath;
+
+ int i = 1;
+ do {
+ filePath = [NSString stringWithFormat:@"%@/%@%03d.%@", docsPath, CDV_PHOTO_PREFIX, i++, self.pickerController.encodingType == EncodingTypePNG ? @"png":@"jpg"];
+ } while ([fileMgr fileExistsAtPath:filePath]);
+
+ // save file
+ if (![self.data writeToFile:filePath options:NSAtomicWrite error:&err]) {
+ result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsString:[err localizedDescription]];
+ }
+ else {
+ result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[[NSURL fileURLWithPath:filePath] absoluteString]];
+ }
+ }
+ else {
+ result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[self.data base64EncodedString]];
+ }
+ if (result) {
+ [self.commandDelegate sendPluginResult:result callbackId:self.pickerController.callbackId];
+ }
+
+ if (result) {
+ [self.commandDelegate sendPluginResult:result callbackId:self.pickerController.callbackId];
+ }
+
+ self.hasPendingOperation = NO;
+ self.pickerController = nil;
+ self.data = nil;
+ self.metadata = nil;
+}
@end