XZ_iOS之加载gif动画

41 篇文章 0 订阅
1、使用 webView 加载本地gif

实现代码:

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代码实现:XZ_iOS之使用WebView实现开机动画效果

2、使用 SDWebImage 加载本地 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))
        
        // 使用 imageData 加载
        imageView.image = UIImage.sd_animatedGIF(with: gif as Data)
        
        view.addSubview(imageView)
3、使用 SDWebImage 加载网络 gif
Swift 4.0之后, 使用 UIImageView + WebCache 类中的 sd_setImageWithURL: placeholderImage: 方法 加载 GIF 图片只能加载第一帧,图片不会动。需要 pod SDWebImage/GIF 框架,并使用 FLAnimatedImageView 作为 imageView 进行加载 gif即可。原因是:4.0版本之后,SDWebImage 使用  FLAnimatedImage 类处理动图。


代码实现:
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];
        }];



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值