{"id":2318,"date":"2013-04-14T01:10:03","date_gmt":"2013-04-14T00:10:03","guid":{"rendered":"http:\/\/t-machine.org\/?p=2318"},"modified":"2013-04-14T01:31:13","modified_gmt":"2013-04-14T00:31:13","slug":"svgkit-scaling-svg-images","status":"publish","type":"post","link":"http:\/\/new.t-machine.org\/index.php\/2013\/04\/14\/svgkit-scaling-svg-images\/","title":{"rendered":"SVGKit: scaling SVG images"},"content":{"rendered":"<p>This is very easy, but lots of people find this difficult <a href=\"http:\/\/t-machine.org\/index.php\/2013\/04\/13\/svg-spec-missing-documentation-the-viewport-and-svg-width\/\">with good reason<\/a>!<\/p>\n<p>Here&#8217;s a quick guide to how image-scaling is implemented with <a href=\"https:\/\/github.com\/SVGKit\/SVGKit\/\">SVGKit<\/a>, so you can effortlessly scale your images.<\/p>\n<p><!--more--><\/p>\n<h3>REQUIRED<\/h3>\n<p>For any of these techniques to work, we need to know the size of your incoming SVG. According to the spec, EVERY SVG file is supposed to have a size, but it is POSSIBLE for an SVG to be saved &#8220;without a size&#8221;.<\/p>\n<p>This is rare, but it does happen.<\/p>\n<p>In that specific case, we CANNOT do some of the things below (the exception is &#8220;scale&#8221; &#8212; we can do this, using a one-liner built-in to SVGKit, but it would be safer for you to fix your SVG file!).<\/p>\n<p>In particular, we definitely CANNOT scale to fit &#8211; because your SVG is infinite. You need to edit the SVG file and set the &#8220;width&#8221; attribute on the root &lt;svg&gt; tag, or you need to set the &#8220;viewBox&#8221; attribute (on the same tag).<\/p>\n<h3>Use 1: Scale-to-fit<\/h3>\n<p>All you have to do is provide a specific size for your SVGKImage, after it&#8217;s finished parsing. It will do the rest for you automatically:<\/p>\n<p>[objc]<br \/>\nSVGKImage im = [SVGKImage imageNamed:&quot;myImage&quot;];<br \/>\nim.size = CGSizeMake( 320, 480 ); \/\/ fill an iOS screen<br \/>\nSVGKImageView* iv = [[SVGKFastImageView alloc] initWithSVGKImage: im];<\/p>\n<p>[self.view addSubview: iv];<br \/>\n[\/objc]<\/p>\n<h3>Use 2: Make it half the size<\/h3>\n<p>In this case, you need to specify a size &#8220;half as big as the default&#8221;.<\/p>\n<p>All you have to do is provide a specific size for your SVGKImage, after it&#8217;s finished parsing. It will do the rest for you automatically:<\/p>\n<p>[objc]<br \/>\nSVGKImage im = [SVGKImage imageNamed:&quot;myImage&quot;];<br \/>\nCGSize originalSize = im.size; \/\/ this defaults to the SVG&#8217;s internal size<br \/>\nfloat half = 1.0 \/ 2.0f;<br \/>\nim.size = CGSizeApplyAffineTransform( im.size, CGAffineTransformMakeScale( half, half ));<br \/>\nSVGKImageView* iv = [[SVGKFastImageView alloc] initWithSVGKImage: im];<\/p>\n<p>[self.view addSubview: iv];<br \/>\n[\/objc]<\/p>\n<h3>Use 3: Make it half the size &#8211; when the SVG is infinite<\/h3>\n<p>If the SVG is infinite, we can&#8217;t make the image half as big &#8211; but we can RENDER it half as big. In this case, it&#8217;ll be up to you to &#8220;guesstimate&#8221; what .frame to place on the SVGKImageView at the end &#8211; or set it to the biggest size you care about rendering?<\/p>\n<p>[objc]<br \/>\nSVGKImage im = [SVGKImage imageNamed:&quot;myImage&quot;];<br \/>\nim.scale = 0.5f; \/\/ NB: this will BE REJECTED if your SVG is non-infinite;<br \/>\n\/\/ &#8230; use the other techniques (listed above) instead!<br \/>\nSVGKImageView* iv = [[SVGKFastImageView alloc] initWithSVGKImage: im];<\/p>\n<p>[self.view addSubview: iv];<br \/>\n[\/objc]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is very easy, but lots of people find this difficult with good reason! Here&#8217;s a quick guide to how image-scaling is implemented with SVGKit, so you can effortlessly scale your images.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"_links":{"self":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts\/2318"}],"collection":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/comments?post=2318"}],"version-history":[{"count":4,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts\/2318\/revisions"}],"predecessor-version":[{"id":2323,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts\/2318\/revisions\/2323"}],"wp:attachment":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/media?parent=2318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/categories?post=2318"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/tags?post=2318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}