Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ios support to convert .dng to .png file #1

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Loading