Skip to content
Merged
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
37 changes: 31 additions & 6 deletions ios/Extensions/UIImage+UsercentricsLogoDict.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,38 @@ import UIKit

extension UIImage {
convenience init?(from dictionary: NSDictionary?) {
guard
let dictionary = dictionary,
let logoName = dictionary["logoName"] as? String
else {
return nil
guard let dictionary = dictionary else { return nil }

if let logoPath = dictionary["logoPath"] as? String, !logoPath.isEmpty,
let image = Self.image(fromLogoPath: logoPath),
let cgImage = image.cgImage {
self.init(cgImage: cgImage, scale: image.scale, orientation: image.imageOrientation)
return
}

if let logoName = dictionary["logoName"] as? String, !logoName.isEmpty {
self.init(named: logoName)
return
}

self.init(named: logoName)
return nil
}

private static func image(fromLogoPath logoPath: String) -> UIImage? {
if logoPath.hasPrefix("file://") {
let path = logoPath.replacingOccurrences(of: "file://", with: "")
if let image = UIImage(contentsOfFile: path) { return image }
}
else if logoPath.hasPrefix("/") {
if let image = UIImage(contentsOfFile: logoPath) { return image }
}
else if logoPath.hasPrefix("http://") || logoPath.hasPrefix("https://"),
let url = URL(string: logoPath),
let data = try? Data(contentsOf: url),
let image = UIImage(data: data) {
return image
}
if let image = UIImage(named: logoPath) { return image }
return nil
}
}
Loading