Skip to content

Commit

Permalink
fix error on ios
Browse files Browse the repository at this point in the history
  • Loading branch information
parveshneedhoo committed Apr 22, 2024
1 parent fe7cfbf commit d5ce77b
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/ios/SpectrumManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#import "SpectrumManager.h"
#import "CDVFile.h"
#import <MobileCoreServices/MobileCoreServices.h>
@implementation SpectrumManager
-(void)compressImage:(CDVInvokedUrlCommand*)command{
[self.commandDelegate runInBackground:^{
Expand All @@ -33,30 +34,29 @@ -(void)startImageCompression:(CDVInvokedUrlCommand*)command{
NSString* sourcePath = config[@"sourcePath"];

if (!sourcePath)
return [self returnErrorResult:command withMsg:@"missing source path"];
return [self returnErrorResult:command withMsg:@"Missing source path"];

sourcePath = [self resolveNativePath: sourcePath];
if (![[NSFileManager defaultManager] fileExistsAtPath:sourcePath])
return [self returnErrorResult:command withMsg:@"source file does not exists"];
return [self returnErrorResult:command withMsg:@"Source file does not exist"];

NSString* currentFolderPath = [sourcePath stringByDeletingLastPathComponent];
NSString* timestampName = [NSString stringWithFormat:@"%f.%@",[[NSDate date] timeIntervalSince1970] * 1000, [sourcePath pathExtension]];
NSString* destinationPath = [currentFolderPath stringByAppendingPathComponent:timestampName];
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);
CGImageSourceRef sourceRef = CGImageSourceCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:sourcePath], NULL);
NSDictionary *metadata = (__bridge NSDictionary *)CGImageSourceCopyPropertiesAtIndex(sourceRef, 0, NULL);

NSError *writeError = nil;
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);
CGImageDestinationRef destinationRef = CGImageDestinationCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:destinationPath], kUTTypeJPEG, 1, NULL);
if (destinationRef) {
CGImageDestinationAddImageFromSource(destinationRef, sourceRef, 0, (CFDictionaryRef)metadata);
CGImageDestinationAddImageFromSource(destinationRef, sourceRef, 0, (__bridge CFDictionaryRef)metadata);
success = CGImageDestinationFinalize(destinationRef);
CFRelease(destinationRef);
}
Expand Down

0 comments on commit d5ce77b

Please sign in to comment.