In the above code, we can see there is also a computed property url that returns the local URL if we provide both the remote URL and the name of the GIF. ![]() Return (forResource: name, withExtension: "gif") url: the URL of the GIF that must be fetched from a server.We’ll create a custom view in SwiftUI called GIFView, which helps us load GIFs from local assets and a remote URL.īefore creating the custom view, let’s create an enum URLType that defines two cases: So, let’s create our custom view to simplify working with FLAnimatedImage! GIFView However, to use FLAnimatedImage in SwiftUI, we have to create a custom view taking advantage of UIViewRepresentable, a wrapper for a UIKit view that we use to integrate it into the SwiftUI view hierarchy. It’s very easy to use FLAnimatedImage with UIKit because it‘s native. / Setting the animated image to the image view Let animatedImage = FLAnimatedImage(animatedGIFData: data) / Creating an animated image from the given animated GIF data Now that we’re familiar with both FLAnimatedImage classes and their usage, we can translate the above Objective-C example into Swift like so: let url = URL(string: "")! We can set the animated image by accessing the animatedImage property. class FLAnimatedImageViewįLAnimatedImageView automatically plays the GIF in the view hierarchy and stops when it’s removed. imageView.animatedImageįLAnimatedImageView is a subclass of UIImageView and takes a FLAnimatedImage. FLAnimatedImage has an initializer that accepts this Data: convenience init(animatedGIFData data: Data!)Īfter creating an instance of an image of the type FLAnimatedImage, we can use it to set the image property of FLAnimatedImageView. To load a GIF image, we convert the GIF into a Data value type. We use it to set the image property of the FLAnimatedImageView class. We want to use FLAnimatedImage with the SwiftUI framework, but before doing so, we should understand this library’s two main classes:įLAnimatedImage is a class that helps deliver the frames in a performant way. The example in the GitHub repo for FLAnimatedImage is written in Objective-C: #import "FLAnimatedImage.h"įLAnimatedImage *image = init] Click continue, and you’ve added FLAnimatedImage to the project! Using FLAnimatedImage with Objective-C ![]() Then, click on Next and select the project target to which the library should be added. Next, paste the following link in the project URL field: To use Swift Package Manager to add FLAnimatedImage to a project, open Xcode, go to the menu bar, and select File > Add Packages. Then, to update only this library, go to your terminal’s project directory and run the following command: carthage update FLAnimatedImage ![]() To add FLAnimatedImage to a project using Carthage, add the following code in the Cartfile: github "Flipboard/FLAnimatedImage" Then, go to your terminal’s project directory and install the pod, like so: pod install To add FLAnimatedImage to a project using CocoaPods, add the following code to the Podfile: pod 'FLAnimatedImage'
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |