{"id":437,"date":"2012-11-19T17:10:04","date_gmt":"2012-11-19T17:10:04","guid":{"rendered":"http:\/\/www.blogs.abeazam.com\/dev\/?p=437"},"modified":"2012-11-21T11:45:55","modified_gmt":"2012-11-21T11:45:55","slug":"vector-graphics-in-ios-with-coregraphics-and-paintcode-from-an-svg-file","status":"publish","type":"post","link":"http:\/\/www.blogs.abeazam.com\/dev\/2012\/11\/vector-graphics-in-ios-with-coregraphics-and-paintcode-from-an-svg-file\/","title":{"rendered":"Vector Graphics in iOS with CoreGraphics and PaintCode from an SVG file"},"content":{"rendered":"<p>Since I started iOS development there has always been one thing missing for me. That has been a vector graphic file format. There is no format currently natively supported by apple.<\/p>\n<p>I did some digging and found <a href=\"https:\/\/github.com\/SVGKit\/SVGKit\">SVGKit<\/a>. This is a very nice library that loads in .svg files. It even supports SVG animations. Not to mention its free. So I had to share this. However it can be tricky to set up.<\/p>\n<p>In the end I settled on using <a href=\"http:\/\/www.paintcodeapp.com\">PaintCode<\/a>. PaintCode is a vector based graphics application that generates CoreGraphics Objective-C. PaintCode will also load in SVG files.<\/p>\n<p>The idea is you load an SVG file into PaintCode. Edit it if needed then copy the Objective-C code into your project.<\/p>\n<p>There are different ways you can include it in your projects. I would like to use it in layer of the CALayer flavour.<\/p>\n<p>What I have done is create a CALayer subclass. That all you have to do is past in the PaintCode code.<\/p>\n<p>For convenience I create all my vector assets at 100&#215;100 pixels. I then use a scale to get it to the right size.<\/p>\n<p>This assumes you have PaintCode and have already got the code ready. Also assumes the image is 100&#215;100.<\/p>\n<p>Step 1:<br \/>\nAdd the &#8220;QuartzCore.framework&#8221; to your project.<\/p>\n<p>Step 2:<br \/>\nCreate a new file using the &#8220;Objective-C Class&#8221; template for iOS. <strong>USE CALayer Subclass<\/strong><\/p>\n<p>Step 3:<br \/>\nAdd the bellow line to to your Header file(.h).<\/p>\n<pre class=\"lang:objc decode:true\">\/\/this method will kick off the drawing\r\n- (void) drawImage;<\/pre>\n<p>&nbsp;<\/p>\n<p>Step 4:<\/p>\n<p>Add the bellow code to the implementation file(.m). <strong>Note:<\/strong> see line 17 if your image is not 100&#215;100 pixels<\/p>\n<pre class=\"lang:objc decode:true\">- (id)init\r\n{\r\n    self = [super init];\r\n    if (self) {\r\n\r\n\t\t[self setNeedsDisplay];\r\n    }\r\n\r\n    return self;\r\n}\r\n\r\n-(void)display\r\n{\r\n\r\n    UIScreen *screen = [UIScreen mainScreen];\r\n    \/\/Sets the hight and width of the image to 100x100 if you have a different image size change here\r\n    CGSize imgSize = CGSizeMake(ceilf(100.0f * self.contentsScale), ceilf(100.0f * self.contentsScale));\r\n\r\n    \/\/\/this is for the retina rescale \r\n    UIGraphicsBeginImageContextWithOptions(imgSize, NO, [screen scale]);\r\n\r\n    CGContextRef context = UIGraphicsGetCurrentContext();\r\n    CGContextScaleCTM(context, self.contentsScale, self.contentsScale);\r\n\r\n    [self drawImage];\r\n\r\n   \/\/grabs the drawn image from the draw context and adds it to UIImage then to the contents of the layer\r\n    UIImage *aImage = UIGraphicsGetImageFromCurrentImageContext();\r\n    UIGraphicsEndImageContext();\r\n    CALayer *curLayer = (CALayer*)self;\r\n\r\n    curLayer.frame = CGRectMake(0, 0, imgSize.width, imgSize.height);\r\n\r\n    curLayer.contents = (id)aImage.CGImage;\r\n\r\n}\r\n\r\n- (void)drawImage\r\n{\r\n\r\n\/\/\/------------------------------\r\n\/\/\/add PaintCode draw code here!!!!\r\n\/\/\/------------------------------\r\n\r\n}<\/pre>\n<p>Step 5:<\/p>\n<p>Add the CoreGraphics code to the drawImage method.<\/p>\n<p>Step 6:<\/p>\n<p>Add this layer to a view that is currently on the view stack.<\/p>\n<pre class=\"lang:default decode:true\">CustomeLayerClass *myLayer = [CustomeLayerClass layer];\r\n\r\n\/\/this will scale the image up 3 fold\r\nmyLayer.contentsScale = 3.0;\r\n\r\n[[[self view] layer] addSublayer:myLayer];<\/pre>\n<p>&nbsp;<\/p>\n<p>Thats all you need.<\/p>\n<p>&nbsp;<\/p>\n<p>**Edit<\/p>\n<p>I created an <a title=\"AppCode File Template for CoreGraphics and PaintCode\" href=\"http:\/\/www.blogs.abeazam.com\/dev\/2012\/11\/appcode-file-template-for-coregraphics-and-paintcode\/\">AppCode template<\/a> for this.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since I started iOS development there has always been one thing missing for me. That has been a vector graphic file format. There is no format currently natively supported by apple. I did some digging and found SVGKit. This is a very nice library that loads in .svg files. It even supports SVG animations. Not [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[89,25,26,28,30,36,37,88,53],"tags":[],"_links":{"self":[{"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/posts\/437"}],"collection":[{"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/comments?post=437"}],"version-history":[{"count":9,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/posts\/437\/revisions"}],"predecessor-version":[{"id":452,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/posts\/437\/revisions\/452"}],"wp:attachment":[{"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/media?parent=437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/categories?post=437"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/tags?post=437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}