Skip to content
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

Closed
BrettAtMangoHealth opened this issue Aug 23, 2018 · 7 comments
Assignees
Milestone

Comments

@BrettAtMangoHealth
Copy link

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.

@szuwest
Copy link

szuwest commented Oct 24, 2018

I hava the same problem,too
I found UIImageExtensions.swift in source code at github, but not in pod sources.

@ystrot
Copy link
Member

ystrot commented Oct 24, 2018

UIImageExtensions is part of the Eugene's PR, so it's not in the pod.

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.

@ystrot ystrot self-assigned this Oct 24, 2018
@ystrot ystrot added this to the 0.9.4 milestone Oct 24, 2018
@dreampiggy
Copy link

dreampiggy commented Oct 30, 2018

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.

@ystrot
Copy link
Member

ystrot commented Oct 30, 2018

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 😊

@ystrot
Copy link
Member

ystrot commented Oct 31, 2018

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.

@szuwest
Copy link

szuwest commented Oct 31, 2018 via email

@ystrot ystrot closed this as completed in 20177d5 Nov 1, 2018
@ystrot
Copy link
Member

ystrot commented Nov 1, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants