Untitled
unknown
swift
a year ago
1.7 kB
2
Indexable
Never
private func getURL(resizeFactor: Int, url: String) -> URL? { /// Regex for pattern matching relevant parts of the URL let pattern = ".*UX([0-9]*)_CR0,([0-9]*),([0-9]*),([0-9]*).*" guard let regex = try? NSRegularExpression(pattern: pattern) else { return nil } let range = NSRange(location: 0, length: url.utf16.count) guard let match = regex.firstMatch(in: url, options: [], range: range) else { return nil } /// Get the image dimensions from the URL guard let range1 = Range(match.range(at: 1), in: url), let range3 = Range(match.range(at: 3), in: url), let range4 = Range(match.range(at: 4), in: url), let imgWidth = Int(String(url[range1])), let containerWidth = Int(String(url[range3])), let containerHeight = Int(String(url[range4])) else { return nil } /// Scaling let imgWidthScaled = imgWidth * resizeFactor let containerWidthScaled = containerWidth * resizeFactor let containerHeightScaled = containerHeight * resizeFactor /// Perform the image dimensions var result = url.replacingOccurrences(of: "(.*UX)([0-9]*)(.*)", with: "$1\(imgWidthScaled)$3", options: .regularExpression) result = result.replacingOccurrences(of: "(.*UX[0-9]*_CR0,[0-9]*,)([0-9]*)(.*)", with: "$1\(containerWidthScaled)$3", options: .regularExpression) result = result.replacingOccurrences(of: "(.*UX[0-9]*_CR0,[0-9]*,[0-9]*,)([0-9]*)(.*)", with: "$1\(containerHeightScaled)$3", options: .regularExpression) let finalURL = URL(string: result) return finalURL }