Skip to content

Commit

Permalink
Merge pull request #1 from mostasim/main
Browse files Browse the repository at this point in the history
feat: add ios support to convert .dng to .png file
  • Loading branch information
ehsanbigzad authored Oct 28, 2024
2 parents 445c062 + 81c478b commit 341e99b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ios/RawImage.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
43 changes: 38 additions & 5 deletions ios/RawImage.swift
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 341e99b

Please sign in to comment.