From 81c478b75ff5542e35203092d1ccc58297599a4f Mon Sep 17 00:00:00 2001 From: Mostasim Billah Date: Fri, 23 Feb 2024 17:26:29 +0600 Subject: [PATCH] feat: add ios support to convert .dng to .png file --- ios/RawImage.m | 2 +- ios/RawImage.swift | 43 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/ios/RawImage.m b/ios/RawImage.m index 51fa45a..bef59df 100644 --- a/ios/RawImage.m +++ b/ios/RawImage.m @@ -2,7 +2,7 @@ @interface RCT_EXTERN_MODULE(RawImage, NSObject) -RCT_EXTERN_METHOD(multiply:(float)a withB:(float)b +RCT_EXTERN_METHOD(rawToPng:(NSString)path withResolver:(RCTPromiseResolveBlock)resolve withRejecter:(RCTPromiseRejectBlock)reject) diff --git a/ios/RawImage.swift b/ios/RawImage.swift index 8c765f5..d79b0eb 100644 --- a/ios/RawImage.swift +++ b/ios/RawImage.swift @@ -1,8 +1,41 @@ @objc(RawImage) class RawImage: NSObject { - - @objc(multiply:withB:withResolver:withRejecter:) - func multiply(a: Float, b: Float, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void { - resolve(a*b) - } + + @objc(rawToPng:withResolver:withRejecter:) + func rawToPng(path: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void { + let imageURL = URL(fileURLWithPath: path) + let imageData = try? Data(contentsOf: imageURL) + if let imageData = imageData, let image = UIImage(data: imageData) { + let outputPath = convertToPNG(image: image) + if ((outputPath?.hashValue) != nil){ + resolve(outputPath) + } + else { + reject("Error converting image", "IMAGE_CONVERTION_ERROR", nil) + } + } else { + reject("Error loading image from path", "IMAGE_LOADING_ERROR", nil) + } + } + + func convertToPNG(image: UIImage) -> String? { + var outputPath : String? = nil + if let jpegImage = image.pngData(){ + + do { + //Convert + let tempDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true) + let newFileName = "\(UUID().uuidString).png" + let targetURL = tempDirectoryURL.appendingPathComponent(newFileName) + try jpegImage.write(to: targetURL, options: []) + outputPath = targetURL.relativePath + }catch { + print (error.localizedDescription) + print("FAILED") + } + }else{ + print("FAILED") + } + return outputPath + } }