iOS 展示网络GIF 图片

news/2024/7/20 23:10:45 标签: ios, 开发语言

方案一 使用sd_webimage

    [self.imgView.imageView sd_setImageWithURL:[NSURL URLWithString:model.topPic]];

方案二 将网络GIF图片下载到沙盒中,然后使用FLAnimationImageView展示

        self.imageView.hidden = YES;
        //        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        if ([self.imageUrl hasPrefix:@"http"])  {
            NSData *data = [NSData dataWithContentsOfFile:self.imageName];
            if (data) {
                self.gifData = data;
            }else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (self.backColor) {
                        self.backgroundColor = self.backColor;
                    }
                });
                [self downLoadImage];
            }
        }else {
            NSString *imagePath = [NSString stringWithFormat:@"%@%@",[TPUserDefault instance].offlinePath,self.imageUrl];
            
            NSData *data = [NSData dataWithContentsOfFile:imagePath];
            if (data) {
                self.gifData = data;
            }else {
                self.gifData = nil;
            }
        }
        //        });

- (void)downLoadImage {
    if (!self.defaultImage) {
        self.image = nil;
    }
    [Remote downloadFileAsync:self.imageUrl actionTag:1000+[getImageNumFromURL(self.imageUrl) intValue] filePath:self.imageName delegate:self];
}

- (void)downloadFileAsync:(NSString*)requestUrl
                actionTag:(int)actionTag
                 filePath:(NSString*)filePath
                 delegate:(id<RemoteDelegate>)delegate {

        NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:requestUrl parameters:nil error:nil];
        [request setAllHTTPHeaderFields:[self requestHeader]];
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer.timeoutInterval = 15;
        NSURLSessionTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
            NSURL *pathURL = [NSURL URLWithString:[@"file://" stringByAppendingString:filePath]];
            return pathURL;
        } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
            if (!error) {
                [self performSelector:@selector(downloadFinish:) withObject:userInfo];
            } else {
                [self handleFailtureResponse:response error:error userInfo:userInfo];
                [self performSelector:@selector(requestFailed:) withObject:userInfo];
            }
        }] ;
        [task resume];
}

- (void)downloadFinish:(NSDictionary *)userInfo {
    @synchronized(self) {
        @try {
            id<RemoteDelegate> delegate = [userInfo objectForKey:@"delegate"];
            int commandTag = [[userInfo objectForKey:@"actionTag"] intValue];
            NSString* fileName = [userInfo objectForKey:@"fileName"];
            
            //通知已成功下载消息给相关代理
            if ([delegate respondsToSelector:@selector(remoteResponsSuccess:withResponsData:)]) {
                [delegate remoteResponsSuccess:commandTag withResponsData:fileName];
            }
        }
        @catch (NSException * e) {
            [self performSelector:@selector(onError:userInfo:)
                       withObject:[e reason]
                       withObject:userInfo];
        }
        @finally {
            [self performSelector:@selector(stopWaitCursor:) withObject:userInfo];
        }
    }
}

请求(下载成功之后执行)成功回调

- (void) remoteResponsSuccess:(int)actionTag withResponsData:(id)resData {
    NSData *imgData = [NSData dataWithContentsOfFile:resData];
    if ([resData isEqualToString:self.imageName]) {
        if ([self.imageName isMatchedByRegex:@".gif"]) {
            self.gifData = imgData;
        }else {
            UIImage* IMAGE = [UIImage imageWithData:imgData];
            self.image = IMAGE;
        }
    }
}

使用 FLAnimatedImage 展示沙盒中的gif 资源

- (void)setGifData:(NSData *)data {
    BOOL isAnimate = NO;
    if (!self.gifView.animatedImage) {
        isAnimate = YES;
        
    }
    _gifData = data;

    FLAnimatedImage *gifImage = [FLAnimatedImage animatedImageWithGIFData:data];

        self.imageView.hidden = YES;
        self.gifView.animatedImage = gifImage;
        self.gifView.hidden = NO;
        self.waterPrint.hidden = YES;
        self.backgroundColor = [UIColor clearColor];
        [self resetGifImageViewLayoutWithImage:gifImage.posterImage];
        
        if ([self.delegate respondsToSelector:@selector(loadImageSuccess:)]) {
            [self.delegate loadImageSuccess:GifImageType];
        }
        if ([self.delegate respondsToSelector:@selector(adXmlLoadImageSuccess:)]) {
            [self.delegate adXmlLoadImageSuccess:nil];
        }
        
        if ([self.delegate respondsToSelector:@selector(loadImageSuccess:withGifData:)]) {
            [self.delegate loadImageSuccess:GifImageType withGifData:data];
        }
        
        if (isAnimate) {
            [CoreAnimationEffect animationEaseIn:self];
        }
    });
}


http://www.niftyadmin.cn/n/4999975.html

相关文章

android studio cmake生成.a文件(静态库)及调用(c c++)静态库.a

第一步生成静态库.a文件&#xff1a; cmake 语法如何生成静态库&#xff0c;就不介绍了&#xff0c;比较简单&#xff0c;我下文列出的参考资料里面有详细介绍。 add_library(${CMAKE_PROJECT_NAME} STATICsrc/CalculStatic.cpp)这一步有坑&#xff0c;我刚开始的时候&#x…

java 实现工厂模式

工厂模式是一种设计模式&#xff0c;用于对象的创建&#xff0c;将对象的创建与使用分离&#xff0c;以提高代码的可维护性和扩展性。在Java中&#xff0c;有三种常见的工厂模式&#xff1a;简单工厂模式、工厂方法模式和抽象工厂模式。 简单工厂模式&#xff1a; 在简单工厂模…

http和https区别,第三方证书如何保证服务器可信

HTTP&#xff08;Hypertext Transfer Protocol&#xff09;和HTTPS&#xff08;HTTP Secure&#xff09;是用于在客户端和服务器之间传输数据的协议&#xff0c;它们在以下几个方面有所区别&#xff1a; 安全性&#xff1a;HTTP是明文协议&#xff0c;数据在传输过程中不加密&…

荣耀开发者沙龙 · 北京站 活动精彩回顾

聚梦想&#xff0c;创非凡&#xff0c;荣耀云业务开发者沙龙北京站顺利落幕。来自全国各地的开发者伙伴齐聚北京&#xff0c;共同探讨在应用分发、服务分发、内容分发上的新可能&#xff0c;探索云业务基础能力和荣耀MagicOS的奥秘&#xff0c;解码商业推广平台的增长潜力&…

基于 Zookeeper 实现服务注册和服务发现

文章目录 前言声明前置知识服务注册和发现Zookeeper 工作原理实现过程注册中心服务注册服务发现 总结 前言 无论是采用SOA还是微服务架构&#xff0c;都需要使用服务注册和服务发现组件。我刚开始接触 Dubbo 时一直对服务注册/发现以及 Zookeeper 的作用感到困惑&#xff0c;现…

如何使用蚂蚁集团自动化混沌工程 ChaosMeta 做 OceanBase 攻防演练?

当前&#xff0c;业界主流的混沌工程项目基本只关注如何制造故障的问题&#xff0c;而经常做演练相关工作的工程师应该明白&#xff0c;每次演练时还会遇到以下痛点&#xff1a; 检测当前环境是否符合演练预设条件&#xff08;演练准入&#xff09;&#xff1b; 业务流量是否满…

9-AJAX-2综合案例

AJAX-综合案例 目录 案例-图书管理图片上传案例-网站换肤案例-个人信息设置 学习目标 今天主要就是练&#xff0c;巩固 axios 的使用 完成案例-图书管理系统&#xff08;增删改查&#xff09;经典业务掌握图片上传的思路完成案例-网站换肤并实现图片地址缓存完成案例-个人信…

Go学习[合集]

文章目录 Go学习-Day1Go学习-Day2标识符变量基础语法字符串类型类型转换string和其他基本类型转换其他类型转stringstring转其他类型 指针类型运算符标准IO分支语句 Go学习-Day3循环语句函数声明init函数匿名函数闭包defer Go学习-Day4函数值传递&#xff0c;引用传递常用的函数…