UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Gif Image Gallery Help

Forums > SwiftUI

So I've been unable to figure out displaying a fixed set of .GIF images in my app. the Xcode Assets folder doesn't seem to like the .GIF file type.

I've found a few UIImage approaches that use an insane amount of code to handle displaying .GIFs but I'm wondering if maybe there's a more up-to-date approach to handle this?

I have a set of pre-saved .GIF images that I want to display in my app but cannot figure out where/how to place and reference them.

2      

It's for Swift 3 so you may run into issues you need to correct, but you could try this: iOSDevCenters+GIF.swift

Never used it myself so I can't say too much about it, but what have you got to lose?

There's also this package: Gifu

3      

@roosterboy

Thank you, I've also come across this package which seems straight forward, but I'm having trouble implementing it.

https://github.com/kirualex/SwiftyGif

I do not understand the UIView and UIViewImage uses. Simply adding this code to a function or extension yields problems.

Mainly it just says that "view" is not in scope, but I do not know why.

import SwiftyGif

do {
    let gif = try UIImage(gifName: "MyImage.gif")
    let imageview = UIImageView(gifImage: gif, loopCount: 3) // Will loop 3 times
    imageview.frame = view.bounds
    view.addSubview(imageview)
} catch {
    print(error)
}

2      

Are you using UIKit or SwiftUI to build your application?

If you are using UIKit, can you post your ViewController code where this is used?

If you are using SwiftUI, can you post your View code where this is used?

2      

I plan to make this a bit more robust than it currently is, but this is the entire Struct. I just get an error that "view" is not in scope on the imageview.frame = view.bounds and view.addSubview(imageview) lines.

Does it need to be in a UIView struct and referenced for display?

import SwiftUI
import SwiftyGif

struct MessageToContacts: View {

    let selectedContacts: Set<Contact>

    @State private var isMessageBuilt = false

    var body: some View {

        ZStack {
            LinearGradient(gradient: Gradient(colors: [Color.purple, Color.blue]), startPoint: .top, endPoint: .bottom)
                .edgesIgnoringSafeArea(.all)

            NavigationLink(
                destination: SendMessages(),
                isActive: $isMessageBuilt,
                label: {
                    EmptyView()
                })
                .opacity(0)

            VStack {
                do {
                    let gif = try UIImage(gifName: "MyImage.gif")
                    let imageview = UIImageView(gifImage: gif, loopCount: 3) // Will loop 3 times
                    imageview.frame = view.bounds
                    view.addSubview(imageview)
                } catch {
                    print(error)
                }
            }

            VStack {
//                whoDat()
                imagePicker()

            }
        }
        .navigationBarTitle("Create your message")
    }
}

2      

Yeah, that code is meant to work with UIKit but you are trying to use it in SwiftUI.

You can load a UIKit UIImage into a SwiftUI Image but it looks like it would just be a static GIF without any looping. It might require the use of a UIViewRepresentable to wrap that UIImageView in order to get full functionality.

Also, you can't use a do...catch block within a SwiftUI View. Having that code in a UIViewRepresentable would solve that problem too.

3      

That code is designed for UIKit, but you're attempting to use it in SwiftUI.

A UIKit UIImage can be loaded into a SwiftUI Image, however it seems to be a static GIF with no looping. To gain complete functionality, you might need to cover the UIImageView with a UIViewRepresentable.

A do...catch block is also not allowed within a SwiftUI View. Being able to use that code in a

3      

Just to close this out. I finally managed to get this going using a different package and some old fashioned coco pod work.

https://cocoapods.org/pods/SSSwiftUIGIFView

Once the dependency is setup, it really is as simple as adding your .gif file to your apps folder structure, and adding this code to draw up the container

SwiftUIGIFPlayerView(gifName: "gif_file_name.gif")

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.