{"id":31274,"date":"2021-01-11T16:35:10","date_gmt":"2021-01-11T21:35:10","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=31274"},"modified":"2021-01-11T16:35:10","modified_gmt":"2021-01-11T21:35:10","slug":"swiftui-views-to-images","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2021\/01\/11\/swiftui-views-to-images\/","title":{"rendered":"SwiftUI Views to Images"},"content":{"rendered":"<p><a href=\"https:\/\/alejandromp.com\/blog\/swiftui-views-to-images\/\">Alejandro Martinez<\/a>:<\/p>\n<blockquote cite=\"https:\/\/alejandromp.com\/blog\/swiftui-views-to-images\/\">\n<p>Sadly, SwiftUI doesn&rsquo;t provide a native way of generating images from its views. We need to resort to tricks used in its ancestor frameworks.<\/p>\n<p>[&#8230;]<\/p>\n<p>First, we need to create a <code>NSHostingView<\/code> with the SwiftUI view that you want to create the image from.<\/p>\n<p>With an <code>NSView<\/code> in hand, the rest of the process is no different that what you would do to rasterize a native AppKit view.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Alejandro Martinez: Sadly, SwiftUI doesn&rsquo;t provide a native way of generating images from its views. We need to resort to tricks used in its ancestor frameworks. [&#8230;] First, we need to create a NSHostingView with the SwiftUI view that you want to create the image from. With an NSView in hand, the rest of the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2021-01-11T21:35:14Z","apple_news_api_id":"8f0ddf20-c2e4-4205-8d09-28c11c77b2dd","apple_news_api_modified_at":"2021-01-11T21:35:15Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/Ajw3fIMLkQgWNCSjBHHey3Q","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[31,1837,30,1891,71,1812],"class_list":["post-31274","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-ios-14","tag-mac","tag-macos-11-0","tag-programming","tag-swiftui"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/31274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=31274"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/31274\/revisions"}],"predecessor-version":[{"id":31275,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/31274\/revisions\/31275"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=31274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=31274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=31274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}