Skip to content

Commit

Permalink
feat: convert imageDataUrlToJpegDataUrl to synchronous method
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielFRico committed Oct 25, 2024
1 parent 79a160d commit 91bc56a
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions ios/EidReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,15 @@ class EIdReader: RCTEventEmitter {

}

enum EidReaderError: Error {
case invalidInput(String)
case processingError(String)
}

@objc(imageDataUrlToJpegDataUrl:withError:)
func imageDataUrlToJpegDataUrl(dataUrl: NSString) throws -> String {
@objc(imageDataUrlToJpegDataUrl:)
func imageDataUrlToJpegDataUrl(dataUrl: NSString) -> String? {
let dataSplit = (dataUrl as String).components(separatedBy: ";base64,")
if(dataSplit.count != 2){
throw EidReaderError.invalidInput("Cannot imageDataUrlToJpegDataUrl image because is not a valid dataurl")
return nil
}
if let mimeType = dataSplit.first?.replacingOccurrences(of: "data:", with: ""){
if(!mimeType.hasPrefix("image/")){
throw EidReaderError.invalidInput("Couldn't convert \(mimeType) to JPEG")
return nil
}
if(mimeType == "image/jpeg"){
return dataUrl as String
Expand All @@ -115,11 +110,10 @@ class EIdReader: RCTEventEmitter {
if let newData = Data(base64Encoded: dataContent) {
if let jpegData = UIImage(data: newData)?.jpegData(compressionQuality: 1.0)?.base64EncodedString(){
return "data:image/jpeg;base64,\(jpegData)"

}
}
}
throw EidReaderError.processingError("Convert image data URL to JPEG image data url error")
return nil
}

@objc(stopReading)
Expand Down

0 comments on commit 91bc56a

Please sign in to comment.