{"id":100,"date":"2012-02-11T05:35:49","date_gmt":"2012-02-11T05:35:49","guid":{"rendered":"http:\/\/ootips.org\/yonat\/?p=100"},"modified":"2016-04-17T07:28:16","modified_gmt":"2016-04-17T07:28:16","slug":"render-uiview-to-uimage","status":"publish","type":"post","link":"https:\/\/ootips.org\/yonat\/render-uiview-to-uimage\/","title":{"rendered":"Render UIView to UIImage"},"content":{"rendered":"<p>There&#8217;s a simple way to render a UIView into a UIImage: use the view&#8217;s <strong>layer<\/strong>, and render it into a bitmap graphic context. Here is the code, phrased as a UIView category:<\/p>\n<pre class=\"lang:objc decode:true \">\r\n#import \"UIView+RenderUIImage.h\"\r\n#import <QuartzCore\/QuartzCore.h>\r\n\r\n@implementation UIView (RenderUIImage)\r\n\r\n- (UIImage *)renderAsImage\r\n{\r\n    \/\/ setup context\r\n    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0f); \/\/ use same scale factor as device\r\n    CGContextRef c = UIGraphicsGetCurrentContext();\r\n\r\n    \/\/ render view\r\n    [self.layer renderInContext:c];\r\n\r\n    \/\/ get reslting image\r\n    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();\r\n    UIGraphicsEndImageContext();\r\n    return result;\r\n}\r\n\r\n@end\r\n<\/pre>\n<p>Just remember to link the QuartzCore.framework and you&#8217;re good to go.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There&#8217;s a simple way to render a UIView into a UIImage: use the view&#8217;s layer, and render it into a bitmap graphic context. Here is the code, phrased as a UIView category: #import &#8220;UIView+RenderUIImage.h&#8221; #import @implementation UIView (RenderUIImage) &#8211; (UIImage &hellip; <a href=\"https:\/\/ootips.org\/yonat\/render-uiview-to-uimage\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[18,7,6,13,14],"class_list":["post-100","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-cocoa","tag-ios","tag-iphone","tag-objective-c","tag-sample-code"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2aEUP-1C","_links":{"self":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts\/100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/comments?post=100"}],"version-history":[{"count":8,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts\/100\/revisions"}],"predecessor-version":[{"id":253,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts\/100\/revisions\/253"}],"wp:attachment":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/media?parent=100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/categories?post=100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/tags?post=100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}