-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mostasim/main
feat: add ios support to convert .dng to .png file
- Loading branch information
Showing
2 changed files
with
39 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |