Reputation: 2874
I'm using PaintCode to convert a set of SVGs I have to use in to Swift code. It looks like it just converts the SVG paths in to UIBezierPath()
s, which is great.
To display the generated code I'm doing the following:
class FirstImageView: UIView {
init(name: String) { // Irrelevant custom init
super.init(frame: CGRectMake(15, 15, 40, 40)) // 40x40 View
self.opaque = false // Transparent background
}
override func drawRect(rect: CGRect) {
ImagesCollection.firstImage() // Fill the view with this image
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
Where ImagesCollection.firstImage()
is referencing:
public class ImagesCollection: NSObject {
public class func firstImage() {
let color4 = UIColor(red: 0.595, green: 0.080, blue: 0.125, alpha: 1.000)
var fill295Path = UIBezierPath()
fill295Path.moveToPoint(CGPointMake(27.5, 2.73))
// Rest of generated graphics code
}
}
Which works great - I generated the graphics at 40x40, set the frame to 40x40, and that works fine. What I'm wondering now, is how can I display that same graphic at a smaller (or larger size) - since they're bezier paths they should scale fine, right? Setting my View's frame to CGRectMake(15, 15, 20, 20)
(for a desired 20x20 image) just seems to clip the graphic.
How can I ensure that whatever graphic is drawn in to my View is sized to the view's frame?
Thanks
Upvotes: 0
Views: 1429
Reputation: 11555
Since you generated the code with PaintCode, you could draw their Frame object around your SVG/Bezier. It would generate the Bezier with dynamic size. In your example, you would get ImagesCollection.firstImage(#frame: CGRect)
instead of ImagesCollection.firstImage()
. You just pass the rectangle from drawRect
to it and you are done.
Upvotes: 1
Reputation: 2874
Slightly off-topic, but per Nate's request here's how I did it with SVGKit:
let svgData: String = "<svg>...</svg>"
let svgParse: SVGKParser = SVGKParser(source: SVGKSource(inputSteam: NSInputStream(data: svgData.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!)))
svgParse.addDefaultSVGParserExtensions()
var svgImage: SVGKImage = SVGKImage(parsedSVG: svgResult, fromSource: nil)
SVGKFastImageView(SVGKImage: svgImage)
Upvotes: 0
Reputation: 93296
You can use a CGAffineTransform
to scale either your custom view or the bezier path itself.
Scale the view:
myFirstImageView.transform = CGAffineTransformMakeScale(0.5, 0.5)
Scale the bezier path:
// ...
// lots of generated graphics code
fill295Path.applyTransform(CGAffineTransformMakeScale(0.5, 0.5))
fill295Path.stroke() // or fill, etc.
Upvotes: 1