Updated Branches: refs/heads/master 5f8f7ef4d -> d8afe9b4a
Fixed CB-506 - images taken via Camera.getPicture do not get deleted Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/commit/d8afe9b4 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/tree/d8afe9b4 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/diff/d8afe9b4 Branch: refs/heads/master Commit: d8afe9b4abf3c8a1049920e2b512b78e581b17cb Parents: 5f8f7ef Author: Shazron Abdullah <shaz...@apache.org> Authored: Wed Jun 6 16:49:42 2012 -0700 Committer: Shazron Abdullah <shaz...@apache.org> Committed: Wed Jun 6 16:49:42 2012 -0700 ---------------------------------------------------------------------- CordovaLib/Classes/CDVCamera.h | 1 + CordovaLib/Classes/CDVCamera.m | 41 ++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/d8afe9b4/CordovaLib/Classes/CDVCamera.h ---------------------------------------------------------------------- diff --git a/CordovaLib/Classes/CDVCamera.h b/CordovaLib/Classes/CDVCamera.h index 6e62232..fa38764 100644 --- a/CordovaLib/Classes/CDVCamera.h +++ b/CordovaLib/Classes/CDVCamera.h @@ -83,6 +83,7 @@ typedef NSUInteger CDVMediaType; */ - (void) takePicture:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options; - (void) postImage:(UIImage*)anImage withFilename:(NSString*)filename toUrl:(NSURL*)url; +- (void) cleanup:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options; - (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info; - (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(NSDictionary*)editingInfo; http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/d8afe9b4/CordovaLib/Classes/CDVCamera.m ---------------------------------------------------------------------- diff --git a/CordovaLib/Classes/CDVCamera.m b/CordovaLib/Classes/CDVCamera.m index 282d8c1..b9c0bad 100644 --- a/CordovaLib/Classes/CDVCamera.m +++ b/CordovaLib/Classes/CDVCamera.m @@ -22,6 +22,8 @@ #import "NSDictionary+Extensions.h" #import <MobileCoreServices/UTCoreTypes.h> +#define CDV_PHOTO_PREFIX @"cdv_photo_" + static NSSet* org_apache_cordova_validArrowDirections; @interface CDVCamera () @@ -164,6 +166,43 @@ static NSSet* org_apache_cordova_validArrowDirections; [cameraPicker release]; } +- (void) cleanup:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options +{ + NSString* callbackId = [arguments objectAtIndex:0]; + + // empty the tmp directory + NSFileManager* fileMgr = [[NSFileManager alloc] init]; + NSError* err = nil; + BOOL hasErrors = NO; + + // clear contents of NSTemporaryDirectory + NSString* tempDirectoryPath = NSTemporaryDirectory(); + NSDirectoryEnumerator* directoryEnumerator = [fileMgr enumeratorAtPath:tempDirectoryPath]; + NSString* fileName = nil; + BOOL result; + + while ((fileName = [directoryEnumerator nextObject])) { + // only delete the files we created + if (![fileName hasPrefix:CDV_PHOTO_PREFIX]) { + continue; + } + NSString* filePath = [tempDirectoryPath stringByAppendingPathComponent:fileName]; + result = [fileMgr removeItemAtPath:filePath error:&err]; + if (!result && err) { + NSLog(@"Failed to delete: %@ (error: %@)", filePath, err); + hasErrors = YES; + } + } + [fileMgr release]; + + if (hasErrors) { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsString:@"One or more files failed to be deleted."]; + [super writeJavascript:[pluginResult toErrorCallbackString:callbackId]]; + } else { + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [super writeJavascript:[pluginResult toSuccessCallbackString:callbackId]]; + } +} - (void) popoverControllerDidDismissPopover:(id)popoverController { @@ -260,7 +299,7 @@ static NSSet* org_apache_cordova_validArrowDirections; int i = 1; do { - filePath = [NSString stringWithFormat:@"%@/photo_%03d.%@", docsPath, i++, cameraPicker.encodingType == EncodingTypePNG ? @"png" : @"jpg"]; + filePath = [NSString stringWithFormat:@"%@/%@%03d.%@", docsPath, CDV_PHOTO_PREFIX, i++, cameraPicker.encodingType == EncodingTypePNG ? @"png" : @"jpg"]; } while ([fileMgr fileExistsAtPath: filePath]);