iOS 展示网络GIF 图片

方案一 使用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];
        }
    });
}
相关推荐
娅娅梨27 分钟前
C++ 错题本--not found for architecture x86_64 问题
开发语言·c++
汤米粥33 分钟前
小皮PHP连接数据库提示could not find driver
开发语言·php
冰淇淋烤布蕾36 分钟前
EasyExcel使用
java·开发语言·excel
拾荒的小海螺42 分钟前
JAVA:探索 EasyExcel 的技术指南
java·开发语言
马剑威(威哥爱编程)1 小时前
哇喔!20种单例模式的实现与变异总结
java·开发语言·单例模式
白-胖-子1 小时前
【蓝桥等考C++真题】蓝桥杯等级考试C++组第13级L13真题原题(含答案)-统计数字
开发语言·c++·算法·蓝桥杯·等考·13级
好睡凯1 小时前
c++写一个死锁并且自己解锁
开发语言·c++·算法
java—大象1 小时前
基于java+springboot+layui的流浪动物交流信息平台设计实现
java·开发语言·spring boot·layui·课程设计
yyqzjw1 小时前
【qt】控件篇(Enable|geometry)
开发语言·qt
csdn_kike2 小时前
QT Unknown module(s) in QT 以及maintenance tool的更详细用法(qt6.6.0)
开发语言·qt