Created
June 24, 2022 07:39
-
-
Save saish98/b848fc279ee0a1f87fb6afc20bcc0f33 to your computer and use it in GitHub Desktop.
Svg image download from url
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Kingfisher+Svg.swift | |
// Created by Saish Chachad on 23/06/22. | |
// | |
import UIKit | |
import Kingfisher | |
import PocketSVG | |
// Convert SVG images from Server to UIImage | |
struct SVGProcessor: ImageProcessor { | |
// `identifier` should be the same for processors with the same properties/functionality | |
// It will be used when storing and retrieving the image to/from cache. | |
let identifier = "svgprocessor" | |
var size: CGSize! | |
init(size: CGSize) { | |
self.size = size | |
} | |
// Convert input data/image to target image and return it. | |
func process(item: ImageProcessItem, options: KingfisherParsedOptionsInfo) -> UIImage? { | |
switch item { | |
case .image(let image): | |
return image | |
case .data(let data): | |
if let svgString = String(data: data, encoding: .utf8) { | |
let path = SVGBezierPath.paths(fromSVGString: svgString) | |
let layer = SVGLayer() | |
layer.paths = path | |
let frame = CGRect(x: 0, y: 0, width: size.width, height: size.height) | |
layer.frame = frame | |
let img = self.snapshotImage(for: layer) | |
return img | |
} | |
return nil | |
} | |
} | |
// Get actual image | |
func snapshotImage(for view: CALayer) -> UIImage? { | |
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.main.scale) | |
guard let context = UIGraphicsGetCurrentContext() else { return nil } | |
view.render(in: context) | |
let image = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return image | |
} | |
} | |
// For SVG rendering only | |
extension UIImageView { | |
/// Use this function for downloading SVG image from URL | |
/// - Parameters: | |
/// - url: SVG image url | |
/// - processor: SVG Image Processor | |
func svgImage(from url: URL?, processor: SVGProcessor) { | |
guard let url = url else { | |
self.image = nil | |
return | |
} | |
KingfisherManager.shared.retrieveImage(with: url, options: [.processor(processor), .forceRefresh]) { result in | |
switch result { | |
case .success(let value): | |
DispatchQueue.main.async { | |
self.image = value.image | |
} | |
case .failure(let error): | |
print("Image download fail:\(error.localizedDescription)") | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TableViewCell.swift | |
// Created by Saish Chachad on 10/06/22. | |
// | |
import UIKit | |
final class TableViewCell: UITableViewCell { | |
// MARK: Outlets | |
@IBOutlet private weak var imgView: UIImageView! | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
// Initialization code | |
} | |
/// Set Data | |
func setData(_ data: urlString) { | |
// Send image size here | |
let processor = SVGProcessor(size: CGSize(width: 17, height: 17)) | |
imgView.svgImage(from: URL(string: urlString), processor: processor) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot! You saved my day:)