{"id":6154,"date":"2021-05-29T08:23:17","date_gmt":"2021-05-29T08:23:17","guid":{"rendered":"https:\/\/www.greytrix.com\/blogs\/salesforce\/?p=6154"},"modified":"2021-07-12T06:59:45","modified_gmt":"2021-07-12T06:59:45","slug":"how-to-dynamically-set-base-64-image-in-lightning-carousel-image","status":"publish","type":"post","link":"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/05\/29\/how-to-dynamically-set-base-64-image-in-lightning-carousel-image\/","title":{"rendered":"How to Dynamically set base 64 image in lightning: carousel image"},"content":{"rendered":"\n<p>In this blog we are going to learn about how to dynamically set image in lightning: carousel. We use this component to display the collection of image. In addition, this component show only one image at a time. For example, let\u2019s start with these component\u2019s attribute.<\/p>\n\n\n\n<p><strong>Src:<\/strong> This attribute stores the path of the image.<\/p>\n\n\n\n<p><strong>Header:<\/strong> The heading of the image element is specified in this attribute.<\/p>\n\n\n\n<p><strong>Description:<\/strong> Image description is provided here.<\/p>\n\n\n\n<p><strong>alternativeText:<\/strong> In case the image is not loaded, this text will be displayed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;lightning:carousel disableAutoScroll=\"true\"&gt;\n&lt;aura:iteration items=\"{!v.Imagelst}\" var=\"tempimg\"&gt;\n&lt;lightning:carouselImage  src = \"{!'data:image\/png;base64,' + tempimg.Image__c}\" header = \"Image\" &gt;\n&lt;\/lightning:carouselImage&gt; \n&lt;\/aura:iteration&gt; \n&lt;\/lightning:carousel&gt;  \n<\/code><\/pre>\n\n\n\n<p>In above example we can take image data into one list i.e. \u201cImagelst \u201d and we can iterate list using aura: iteration.  \u201cImagelst \u201d is a wrapper object. It&#8217;s used to wrap the carousel components together, other features like header, description etc can also be changed depending on the requirement.\u00a0We can store base 64 string into object record OR we can directly get image from static resource also.<\/p>\n\n\n\n<p>\u201cdata: image\/png;base64\u201d is an inlined image (png), encoded in base64. It can make a page faster ,the browser doesn&#8217;t have to query the server for the image data separately, saving a round trip.<\/p>\n\n\n\n<p>For instance, to use images in your org, upload them as static resources in the Static Resources page in Setup. In addition, to reference a resource in\u00a0\u201c<code>lightning:carouselImage\u201d<\/code>, use\u00a0\u201c<code>{!$Resource.carousel}\"<\/code>\u00a0syntax in your\u00a0<code>src<\/code>\u00a0attribute, where\u00a0<code>image<\/code>\u00a0is the name you provided in the\u00a0<code>Name<\/code>\u00a0field on the setup page.<\/p>\n\n\n\n<p>If the image path is invalid or the image does not load, because the user is offline or any other reason, the description and alternative text are shown in place of the image.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><span style=\"text-decoration: underline;\"><em>Example<\/em><\/span><\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;aura:component implements=\"force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction\" access=\"global\" >\r\n    &lt;aura:attribute name=\"lstimg\" type=\"Object&#91;]\"\/>\r\n    &lt;aura:handler name=\"init\" action=\"{!c.listAction}\" value=\"{!this}\"\/>\r\n    &lt;div class=\"slds-align_absolute-center\" style=\"height: 5rem;\">\r\n        &lt;div class=\"slds-size_1-of-2\">\r\n            &lt;div class=\"slds-box slds-align_absolute-center\" style=\"height: 5rem; position: absolute; width: 300px; height: 200px; z-index: 15; top: 50%; left: 50%; margin: -100px 0 0 -150px; border-color: rgb(255, 255, 255);\" >\r\n                &lt;lightning:carousel disableAutoScroll=\"false\">\r\n                    &lt;aura:iteration items=\"{!v.lstimg}\" var=\"img\">\r\n                        &lt;lightning:carouselImage width=\"10px\" height=\"10px\" src = \"{!img.image}\" header = \"{!img.Header}\" description = \"{!img.Description}\" alternativeText = \"{!img.AlterText}\" href = \"{!img.imgUrl}\">\r\n                        &lt;\/lightning:carouselImage>\r\n                    &lt;\/aura:iteration>\r\n                  &lt;\/lightning:carousel>\r\n            &lt;\/div>\r\n        &lt;\/div>\r\n    &lt;\/div>\r\n&lt;\/aura:component>\n\n\n({\r\n\tlistAction : function(component, event, helper) \r\n\t{\r\n\t\t\/\/change the image names,header,description etc as required\r\n\t\tvar name=&#91;'Carousel1','Carousel2','Carousel3'];\r\n\t\tvar header=&#91;'Card1','Card2','Card3'];\r\n\t\tvar description=&#91;'Description1','Description2','Description3'];\r\n\t\tvar AlternativeText=&#91;'Text1','Text2','Text3'];\r\n\t\tvar ImageUrl=&#91;'https:\/\/www.salesforce.com','https:\/\/www.salesforce.com','https:\/\/www.salesforce.com'];\r\n\t\tvar b=&#91;'1','2','3','4','5'];\r\n\t\tvar list1=&#91;];\r\n\t\tvar a=name.length;\r\n\t\tfor(var i=0;i&lt;a;i++)\r\n\t\t{\r\n\t\t\tlist1.push({image:$A.get('$Resource.'+name&#91;i]),Header:header&#91;i],Description:description&#91;i],AlterText:AlternativeText&#91;i],imgUrl:ImageUrl&#91;i]}) ;\r\n\t\t}\r\n\t\tcomponent.set(\"v.lstimg\",list1);\r\n\t}\r\n})\r\n\r<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/05\/Carousel-Image-Output.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/05\/Carousel-Image-Output.png\" alt=\"Carousel Image Output\"><\/a><\/center>\n<font size=\"2\"><center><i>Carousel Image Output<\/i><\/center><\/font>\n\n\n\n<p>In conclusion, we can dynamically display image by using Lightning:carousel component and\u00a0lightning:carouselImage component.<\/p>\n\n\n\n<p>We hope you may find this blog resourceful and helpful. If you still have concerns and need more help, please contact us at <a href=\"mailto:salesforce@greytrix.com\">salesforce@greytrix.com<\/a><\/p>\n\n\n\n<p style=\"text-align: justify\"><b>About Us<\/b><\/br>\n<p><a href=\"https:\/\/www.greytrix.com\/\">Greytrix<\/a> \u2013 a globally recognized and one of the oldest Sage Development Partner and a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners and Sage PSG across the globe. We offer Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that require apex code incorporation into the Salesforce platform.<br><br> Greytrix has some unique solutions for Cloud CRM such as <a href=\"\">Salesforce Sage integration<\/a> for <a href=\"https:\/\/www.greytrix.com\/sage-x3-erp\/integration\/\">Sage X3<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-100-integration\/\">Sage 100<\/a> and <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-300-integration\/\">Sage 300 (Sage Accpac)<\/a>. We also offer best-in-class Cloud CRM <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Salesforce customization and development services<\/a> along with services such as Salesforce <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/data-migration-support\/\">Data Migration<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Integrated App development<\/a>, Custom App development and Technical Support business partners and end users. Salesforce Cloud CRM integration offered by Greytrix works with Lightning web components and supports standard opportunity workflow. Greytrix GUMU&#x2122; integration for Sage ERP \u2013 Salesforce is a 5-star rated app listed on <a href=\"https:\/\/appexchange.salesforce.com\/appxListingDetail?listingId=a0N30000000psM5EAI\" target=\"_blank\" rel=\"noopener\">Salesforce AppExchange<\/a>.<br> The GUMU&#x2122; Cloud framework by Greytrix forms the backbone of cloud integrations that are managed in real-time for processing and execution of application programs at the click of a button.<br><br> For more information on our Salesforce products and services, contact us at <a href=\"mailto:salesforce@greytrix.com\">salesforce@greytrix.com<\/a>. We will be glad to assist you.<\/p>\n\n\n\n<p><strong>Related Posts:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/03\/30\/how-to-use-lightning-component-in-visualforce-page\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Use Lightning Component in Visualforce page<\/a><\/li><li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/01\/22\/salesforce-how-to-get-row-index-of-lightning-table-rows\/\" target=\"_blank\" rel=\"noreferrer noopener\">Salesforce \u2013 How to get row index of lightning table rows<\/a><\/li><li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/03\/17\/how-to-debug-in-lightning-using-developer-console-api\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to debug in lightning using developer console API<\/a><\/li><li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/01\/20\/how-to-add-lightning-web-components-as-custom-tabs-in-salesforce\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to add Lightning Web Components as Custom Tabs in Salesforce<\/a><\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog we are going to learn about how to dynamically set image in lightning: carousel. We use this component to display the collection of image. In addition, this component show only one image at a time. For example, let\u2019s start with these component\u2019s attribute. Src: This attribute stores the path of the image.\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/05\/29\/how-to-dynamically-set-base-64-image-in-lightning-carousel-image\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[1131,1068,1138,1140,206,1055,1069,207,1134,1135,208,651],"class_list":["post-6154","post","type-post","status-publish","format-standard","hentry","category-salesforce-srv","tag-aura","tag-aura-method","tag-carousel-image-example","tag-carousel-image-in-salesforce","tag-lightning","tag-lightning-aura-component","tag-lightning-aura-method","tag-lightning-basics","tag-lightning-carousel","tag-lightning-carousel-image","tag-lightning-component","tag-salesforce-lightning-component"],"_links":{"self":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/comments?post=6154"}],"version-history":[{"count":2,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6154\/revisions"}],"predecessor-version":[{"id":6157,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6154\/revisions\/6157"}],"wp:attachment":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/media?parent=6154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/categories?post=6154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/tags?post=6154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}