<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Gif &#8211; 隨心所欲</title>
	<atom:link href="https://doeverythingiwant.com/tag/gif/feed/" rel="self" type="application/rss+xml" />
	<link>https://doeverythingiwant.com</link>
	<description>iOS Developer 的隨筆記錄</description>
	<lastBuildDate>Sun, 04 Feb 2024 08:41:00 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1</generator>

<image>
	<url>https://doeverythingiwant.com/wp-content/uploads/2025/08/cropped-0802.png</url>
	<title>Gif &#8211; 隨心所欲</title>
	<link>https://doeverythingiwant.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>SwiftUI 與 Gif</title>
		<link>https://doeverythingiwant.com/swiftui-gif/</link>
					<comments>https://doeverythingiwant.com/swiftui-gif/#respond</comments>
		
		<dc:creator><![CDATA[艾普利]]></dc:creator>
		<pubDate>Sun, 04 Feb 2024 08:40:58 +0000</pubDate>
				<category><![CDATA[寫程式]]></category>
		<category><![CDATA[Gif]]></category>
		<category><![CDATA[SwiftUI]]></category>
		<guid isPermaLink="false">https://doeverythingiwant.com/?p=2191</guid>

					<description><![CDATA[Photo by Євгенія Височина on U&#8230;]]></description>
										<content:encoded><![CDATA[
<p class="has-text-align-center wp-block-paragraph">Photo by <a href="https://unsplash.com/@eugenivy_now?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash" target="_blank" rel="noopener">Євгенія Височина</a> on <a href="https://unsplash.com/photos/a-group-of-wrapped-presents-under-a-christmas-tree-bh1fKYXv2cA?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash" target="_blank" rel="noopener">Unsplash</a></p>



<p class="wp-block-paragraph">SwiftUI 本身的Image 並沒有支援 Gif 檔案，若是想要放動畫的除了用程式去寫外，現在有 Lottie 可以使用，SwiftUI 其實也是可以使用 Gif Image ，不過畢需要透過WKWebView 來實現</p>



<span id="more-2191"></span>



<p class="wp-block-paragraph">先找一個gif 圖，本次使用的是這個櫻花的圖示</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img fetchpriority="high" decoding="async" width="256" height="256" src="https://doeverythingiwant.com/wp-content/uploads/2023/12/cherry-blossoms.gif" alt="" class="wp-image-2193"/><figcaption class="wp-element-caption">GIF by <a href="https://pixabay.com/users/viaductk-34217157/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=animation&amp;utm_content=6383" target="_blank" rel="noopener">viaductk</a> from <a href="https://pixabay.com//?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=animation&amp;utm_content=6383" target="_blank" rel="noopener">Pixabay</a></figcaption></figure>
</div>


<p class="wp-block-paragraph">這個檔案需要放在專案裡面，不需要放在 Assets 因為它並不是單純的圖片</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="454" height="388" src="https://doeverythingiwant.com/wp-content/uploads/2023/12/000.png" alt="" class="wp-image-2194" srcset="https://doeverythingiwant.com/wp-content/uploads/2023/12/000.png 454w, https://doeverythingiwant.com/wp-content/uploads/2023/12/000-300x256.png 300w" sizes="(max-width: 454px) 100vw, 454px" /></figure>
</div>


<p class="wp-block-paragraph"><code>WKWebView</code> 是UIView 現在屬於WebKit 底下，UIView 是屬於 UIKit 底下，而UIKit元件要使用在 SwiftUI 上需要用<code>UIViewRepresentable</code> 做處理，官方有一個<a href="https://developer.apple.com/tutorials/swiftui/interfacing-with-uikit" target="_blank" rel="noreferrer noopener">教學</a>，有興趣的可以看一看，最主要就是需要實作二個function</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-swift" data-lang="Swift"><code>func makeUIView(context: Context) -&gt; UIView { }
func updateUIView(_ uiView: UIView, context: Context) { }</code></pre></div>



<p class="wp-block-paragraph">在有<code>Delegate</code> 的情況就是要實作 <code>class Coordinator</code>，詳細可以看教學或是上網找一下相關教學</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">實作</h2>



<p class="wp-block-paragraph">這次主要使用的是 <code>WKWebView</code> 因此上述 <code>UIView</code> 的部分都需要改成 <code>WKWebView</code></p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-swift" data-lang="Swift"><code>func makeUIView(context: Context) -&gt; WKWebView {
    let webView = WKWebView()
    let url = Bundle.main.url(forResource: imageName, withExtension: &quot;gif&quot;)!
    let data = try! Data(contentsOf: url)
        
    webView.load(data, mimeType: &quot;image/gif&quot;, characterEncodingName: &quot;UTF-8&quot;, baseURL: url.deletingLastPathComponent())
        
    return webView
}

func updateUIView(_ uiView: WKWebView, context: Context) {
    uiView.reload()
}</code></pre></div>



<p class="wp-block-paragraph">剛剛有說過 <code>WKWebView</code> 是屬於 WebKit 底下，所以要記得 <code>import WebKit</code>，簡單說明一下程式碼</p>



<ul class="wp-block-list">
<li>建立一個 <code>WKWebView</code> 做為回傳值</li>



<li><code>url</code> 參數取得gif 位置，<code>forResource</code> 是代入圖片名稱，因為希望能任意填入名稱，所以使用一個參數代表 image name，<code>Bundle.main.url()</code> 的回傳值是 Optional ，由於Gif 圖示一定要放在檔案裡，基本上不會有nil 的情況，所以就直接最後加上 !</li>



<li><code>data</code> 參數將圖片轉成 data，回傳值也是Optional的，如上述所說，這裡也直接使用 <code>try!</code> 來處理</li>



<li>最後讓webView 去取讀這個檔案
<ul class="wp-block-list">
<li><code>mimeType</code> 就是指 Internet media type，寫web 的朋友們應該都很清楚，這是用來表示這是什麼樣的檔案，在此當然是 &#8220;image/gif&#8221;，有興趣可以看一下<a href="https://zh.wikipedia.org/zh-tw/%E4%BA%92%E8%81%94%E7%BD%91%E5%AA%92%E4%BD%93%E7%B1%BB%E5%9E%8B" target="_blank" rel="noreferrer noopener">維基百科</a>的說明</li>



<li><code>characterEncodingName</code>，使用 UTF-8 來做解碼方式</li>
</ul>
</li>



<li><code>updateUIView()</code> 只需要做一件事，就是 <code>reload()</code> 就好了</li>
</ul>



<p class="wp-block-paragraph">這樣就完成啦~ 很簡單對吧~ </p>



<p class="wp-block-paragraph">最後的結果就像這樣，加上 .frame 就可以調整大小，使用上非常方便</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-swift" data-lang="Swift"><code>VStack {
   GifImageView(imageName: &quot;cherry-blossoms&quot;)
       .frame(width: 60, height: 60)
   Text(&quot;Hello, world!&quot;)
}
.padding()</code></pre></div>


<div class="wp-block-image wp-duotone-unset-1">
<figure class="aligncenter size-full"><img decoding="async" width="295" height="640" src="https://doeverythingiwant.com/wp-content/uploads/2023/12/demo.gif" alt="" class="wp-image-2199"/></figure>
</div>


<p class="wp-block-paragraph">不知道未來 SwiftUI 會不會發展出自己的 WebView ，或是 Image 之後也可以支援 Gif 檔案，在那之前就用 WKWebView 來呈現Gif 圖示吧</p>



<p class="wp-block-paragraph">最後祝大家 Coding 愉快!!</p>



<p class="wp-block-paragraph">可以在<a href="https://github.com/AprilXMoon/GifImage" target="_blank" rel="noreferrer noopener">這裡</a>取得此Demo 檔案喔~</p>
]]></content:encoded>
					
					<wfw:commentRss>https://doeverythingiwant.com/swiftui-gif/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
