Skip to content

Commit

Permalink
Merge pull request #9 from guhungry/Feature/add-support-base64-encode…
Browse files Browse the repository at this point in the history
…d-image

feat(FileUtils): add FileUtils.imageFromString() to support base64 encoded image
  • Loading branch information
guhungry authored Aug 11, 2024
2 parents c6ed9e9 + 036161e commit c8706c7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
23 changes: 23 additions & 0 deletions WCPhotoManipulator/FileUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ import UIKit
}
return UIImage(data: data)
}

@objc public class func imageFromString(_ url: String) -> UIImage? {
guard isBase64Image(url) else {
guard let url = URL(string: url) else {
return nil
}
return imageFromUrl(url)
}

guard let commaIndex = url.firstIndex(of: ",") else {
return nil
}

let dataIndex = url.index(after: commaIndex)
guard let data = Data(base64Encoded: String(url[dataIndex...])) else {
return nil
}
return UIImage(data: data)
}

private class func isBase64Image(_ url: String) -> Bool {
return url.starts(with: "data:")
}

@objc internal class func imageToData(_ image: UIImage, mimeType: String, quality: CGFloat) -> Data? {
if (MimeUtils.PNG == mimeType) {
Expand Down
Loading

0 comments on commit c8706c7

Please sign in to comment.