Skip to content

Commit

Permalink
compress and copy exif ios
Browse files Browse the repository at this point in the history
  • Loading branch information
parveshneedhoo committed Apr 12, 2024
1 parent 81961ec commit fe7cfbf
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/ios/SpectrumManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,32 @@ -(void)startImageCompression:(CDVInvokedUrlCommand*)command{
UIImage* image = [UIImage imageWithContentsOfFile:sourcePath];
NSData *compressedImageData = UIImageJPEGRepresentation(image, 0.8);
NSError *writeError = nil;


CGImageSourceRef sourceRef = CGImageSourceCreateWithData((CFDataRef)compressedImageData, NULL);
NSDictionary *metadata = (NSDictionary *)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(sourceRef, 0, NULL));
CFRelease(sourceRef);

BOOL success = [compressedImageData writeToFile:destinationPath options:NSDataWritingAtomic error:&writeError];
if (!success) {
return [self returnErrorResult:command withMsg:writeError.localizedDescription];
} else {
CGImageDestinationRef destinationRef = CGImageDestinationCreateWithURL((CFURLRef)[NSURL fileURLWithPath:destinationPath], (CFStringRef)@"public.jpeg", 1, NULL);
if (destinationRef) {
CGImageDestinationAddImageFromSource(destinationRef, sourceRef, 0, (CFDictionaryRef)metadata);
success = CGImageDestinationFinalize(destinationRef);
CFRelease(destinationRef);
}

if (!success) {
return [self returnErrorResult:command withMsg:@"Failed to copy metadata"];
}

NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager removeItemAtPath:sourcePath error:nil]) {
[fileManager copyItemAtPath:destinationPath toPath:sourcePath error:nil];
[fileManager removeItemAtPath:destinationPath error:nil];
}

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[pluginResult setKeepCallback:@YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
Expand Down

0 comments on commit fe7cfbf

Please sign in to comment.