-
Notifications
You must be signed in to change notification settings - Fork 561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Getting a UIImage from the SVG file on a background thread without using a MacawView. #468
Comments
I hava the same problem,too |
For now you can use following sample: #382 (comment), but it doesn't work in background thread. To make it possible in background we need to change our rendering system. So we keeping this feature in mind and will implement it once it can be possible. |
So....Does this means my dream of using your awesome SVG engine to polish our SVG coder plugin SDWebImageSVGCoder is impossible 😅 Currentlly we have to use SVGKit, which contains some bug for rendering the modern SVG files, but at least works for background rendering mode. It seems that framework is lack of maintain. And found your awesome SVG ending which have the great community. |
Hey @dreampiggy. Thank you for the kind words! This task is one of the first in our roadmap, so I hope your dream will come true in the nearest time 😊 |
Hey guys! Can anybody provide a sample showing that override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.global(qos: .background).async {
let image = self.svgToImage(resourceName: "logo", size: CGSize(width: 100, height: 100))
DispatchQueue.main.async {
self.view!.backgroundColor = UIColor(patternImage: image)
}
}
}
func svgToImage(resourceName: String, size: CGSize) -> UIImage {
let rootNode = try! SVGParser.parse(resource: resourceName)
let macawView = MacawView(node: rootNode, frame:CGRect(origin: CGPoint.zero, size: size))
UIGraphicsBeginImageContext(size)
macawView.layer.render(in: UIGraphicsGetCurrentContext()!)
let img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img!
} But it works fine for me. |
it is not safe.sometime it is ok,sometime it will crash
Yuri Strot <[email protected]>于2018年10月31日 周三下午8:38写道:
… Hey guys! Can anybody provide a sample showing that MacawView doesn't
work in a background thread? I was trying to do it this way:
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.global(qos: .background).async {
let image = self.svgToImage(resourceName: "logo", size: CGSize(width: 100, height: 100))
DispatchQueue.main.async {
self.view!.backgroundColor = UIColor(patternImage: image)
}
}
}
func svgToImage(resourceName: String, size: CGSize) -> UIImage {
let rootNode = try! SVGParser.parse(resource: resourceName)
let macawView = MacawView(node: rootNode, frame:CGRect(origin: CGPoint.zero, size: size))
UIGraphicsBeginImageContext(size)
macawView.layer.render(in: UIGraphicsGetCurrentContext()!)
let img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img!
}
But it works fine for me.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#468 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ACar-dneq3LBm9j8-XXC2JImzz38vSMSks5uqZm4gaJpZM4WKWuT>
.
|
Now you can convert any node to a UIImage using following method: try! SVGParser.parse(resource: resourceName).toNativeImage(size: imageSize) You can also specify content layout as an argument to align a Node/SVG. |
Greetings,
Thank you for the work provided in the Macaw library.
Our team is trying to do something similar to this open PR #382. We are trying to collect a UIImage from the SVG file on a background thread. Attempting to use the code snippet provided by @ystrot, the resulting image is always blank or empty. I was wondering if there is a solution to this without using MacawViews.
Thank you for your time. If there needs to be any clarification, please ask.
The text was updated successfully, but these errors were encountered: