How to Load Gif Image in Swift

How to load GIF image in Swift?

Load GIF image Swift :

## Reference.

#1 : Copy the swift file from This Link
:

#2 : Load GIF image Using Name

    let jeremyGif = UIImage.gifImageWithName("funny")
let imageView = UIImageView(image: jeremyGif)
imageView.frame = CGRect(x: 20.0, y: 50.0, width: self.view.frame.size.width - 40, height: 150.0)
view.addSubview(imageView)

#3 : Load GIF image Using Data

    let imageData = try? Data(contentsOf: Bundle.main.url(forResource: "play", withExtension: "gif")!)
let advTimeGif = UIImage.gifImageWithData(imageData!)
let imageView2 = UIImageView(image: advTimeGif)
imageView2.frame = CGRect(x: 20.0, y: 220.0, width:
self.view.frame.size.width - 40, height: 150.0)
view.addSubview(imageView2)

#4 : Load GIF image Using URL

    let gifURL : String = "http://www.gifbin.com/bin/4802swswsw04.gif"
let imageURL = UIImage.gifImageWithURL(gifURL)
let imageView3 = UIImageView(image: imageURL)
imageView3.frame = CGRect(x: 20.0, y: 390.0, width: self.view.frame.size.width - 40, height: 150.0)
view.addSubview(imageView3)

Download Demo Code

OUTPUT :

iPhone 8 / iOS 11 / xCode 9

Sample Image

Loading GIFs in Xcode using Swift

you have option like https://cocoapods.org/pods/Gifu framework as well

iOS Swift How To Use Gif File Inside My Project

Download UIImage+Gif.swift file from https://github.com/bahlo/SwiftGif/tree/master/SwiftGifCommon and put into your project..

// An animated UIImage
let Gif = UIImage.gif(name: "jerry")

// A UIImageView with async loading
let imageView = UIImageView()
imageView.loadGif(name: "tom")]

demo

How to use GIFs inside an iOS App without any Third Party Library in Swift 5 using UIKit?

You have to manually decode GIF data, then you will be able to show it frame-by-frame. You will not have any simple answers here, bcs that's definitely very hard idea, try copy decoding algorithm from Third-Party and player or just use it. Something like SDWebImage will be the best option for you. Check SDAnimatedImagePlayer, SDWebAnimatedImage classes.

SwiftUI display gif image

Easiest and fastest way to display gif image in swiftUI - is to use Preview / QuickLook (QL) / QLPreviewView

Quartz available only in macOS 10.4+ https://developer.apple.com/documentation/quartz

import SwiftUI
import Quartz

struct QLImage: NSViewRepresentable {
var url: URL

func makeNSView(context: NSViewRepresentableContext) -> QLPreviewView {
let preview = QLPreviewView(frame: .zero, style: .normal)
preview?.autostarts = true
preview?.previewItem = url as QLPreviewItem

return preview ?? QLPreviewView()
}

func updateNSView(_ nsView: QLPreviewView, context: NSViewRepresentableContext) {
nsView.previewItem = url as QLPreviewItem
}

typealias NSViewType = QLPreviewView
}

How to add GIF images to Assets folder and load them in UIImageView programmatically

According to your comment, this is the solution that you are searching:

  1. make an extension of UIImage which uses SwiftGif:

    extension UIImage {
    public class func gif(asset: String) -> UIImage? {
    if let asset = NSDataAsset(name: asset) {
    return UIImage.gif(data: asset.data)
    }
    return nil
    }
    }
  2. Then tuning @Ganesh suggestion with the extension you might do something like:

    imageView.image = UIImage.gif(asset: "YOUR_GIF_NAME_FROM_ASSET")

How to display GIF in app?

As @Patrick suggested, you have to replace your image path from asset folder and move it in your app’s bundle. This issue is also raised in its official git library’s issue tab. https://github.com/bahlo/SwiftGif/issues/67.

Unable to show a *.gif file in a UIImageView in swift

I strongly recommend you to use SwiftGif.

Import the Gif.swift in your project and do the following:

// Returns an animated UIImage
let jeremyGif = UIImage.gifWithName("jeremy")

// Use the UIImage in your UIImageView
let imageView = UIImageView(image: jeremyGif)

Gif disappears in UITableViewCell

I'm going to show all stages of showing animated gif on the UITableViewCell

1) This is my GifTableViewController that contains a UITableView

import UIKit

class GifTableViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

var gifs = [String]()

override func viewDidLoad() {
super.viewDidLoad()

loadGifs()
}

func loadGifs() {
gifs.append("https://media.giphy.com/media/XIqCQx02E1U9W/giphy.gif")
gifs.append("https://media.giphy.com/media/11JTxkrmq4bGE0/giphy.gif")
gifs.append("https://media.giphy.com/media/eoxomXXVL2S0E/giphy.gif")
gifs.append("https://media.giphy.com/media/c5wbvuaVVLWzC/giphy.gif")
gifs.append("https://media.giphy.com/media/l9Jhzwdi09Ve0/giphy.gif")
gifs.append("https://media.giphy.com/media/8h1Zhv62CVXEc/giphy.gif")
gifs.append("https://media.giphy.com/media/FgiHOQyKUJmwg/giphy.gif")
gifs.append("https://media.giphy.com/media/h2MLtoOjxtkGY/giphy.gif")
gifs.append("https://media.giphy.com/media/ClKnUxoh4SP16/giphy.gif")
gifs.append("https://media.giphy.com/media/S6fA9ppFTwFhK/giphy.gif")
gifs.append("https://media.giphy.com/media/EGiBhTZMXedIA/giphy.gif")
}

}


extension GifTableViewController: UITableViewDataSource, UITableViewDelegate {

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return gifs.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "GifTableCell", for: indexPath) as! GifTableCell
cell.load(with: gifs[indexPath.row])
return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}

}

2) This is my GifTableCell that contains a UIImageView which will represent gif on

import UIKit

class GifTableCell: UITableViewCell {

@IBOutlet weak var gifImageView: UIImageView!

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

func load(with urlString: String) {
gifImageView.image = nil
DispatchQueue.global().async { [weak self] in
guard let url = URL(string: urlString as String) else {
return
}
guard let data = try? Data(contentsOf: url) else {
return
}

DispatchQueue.main.async {
self?.gifImageView.image = UIImage.gif(data: data)
}
}
}

}

3) Notice that UIImage.gif(data: data) statement. The gif function is an UIImage extension from the SwiftGifOrigin library

See source: https://github.com/swiftgif/SwiftGif

You can add only UIImage+Gif.swift file in order to use by simply, or include SwiftGifOrigin library into your project.

Edits for question update;

The above example shows that gifs are loaded from url. Your case is actually much simpler, your cell should be like this.

class CustomCell: UITableViewCell{

@IBOutlet weak var theImageView: UIImageView!

func loadGif() {
theImageView.image = nil
DispatchQueue.global().async { [weak self] in
let loadingGif = UIImage.gifImageWithName("loading")
DispatchQueue.main.async {
self?.theImageView.image = loadingGif
}
}
}

}


Related Topics



Leave a reply



Submit