1. Use webView to load local gif
Implementation code:
guard let bundle = Bundle.main.path(forResource: "duck.gif", ofType: nil), let gif = NSData.init(contentsOfFile: bundle) else { return } let webView = UIWebView.init(frame: CGRect(x: 50, y: 100, width: 280, height: 200)) view.addSubview(webView) let url = URL(fileURLWithPath: bundle) webView.load(gif as Data, mimeType: "image/gif", textEncodingName: String(), baseURL: url)
OC code implementation: XZ_iOS uses WebView to achieve boot animation effect
2. Use SDWebImage to load local gif
guard let bundle = Bundle.main.path(forResource: "duck.gif", ofType: nil), let gif = NSData.init(contentsOfFile: bundle) else { return } let imageView = UIImageView.init(frame: CGRect(x: 50, y: 100, width: 280, height: 200)) // load using imageData imageView.image = UIImage.sd_animatedGIF(with: gif as Data) view.addSubview(imageView)
3. Use SDWebImage to load network gif
After Swift 4.0, using the sd_setImageWithURL: placeholderImage: method in the UIImageView + WebCache class to load a GIF image can only load the first frame, and the image will not move. You need pod SDWebImage/GIF framework, and use FLAnimatedImageView as imageView to load gif. The reason is: After version 4.0, SDWebImage uses the FLAnimatedImage class to process animations.
Code:
FLAnimatedImageView *_imageView; _imageView.frame = CGRectMake(0, 0, size.width, size.height); [_imageView sd_setImageWithURL:_url placeholderImage:_placeholder options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL* targetUrl) { dispatch_async(dispatch_get_main_queue(), ^{ _progressView.progress = (float)receivedSize / expectedSize; }); } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { if (image == nil) { return; } [self setImagePosition:image]; }];