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];
        }
    });
}
相关推荐
liuluyang53044 分钟前
C语言C11支持的结构体嵌套的用法
c语言·开发语言·算法·编译·c11
凌叁儿1 小时前
python保留关键字详解
开发语言·python
明飞19872 小时前
C_内存 内存地址概念
c语言·开发语言
代码不停2 小时前
Java中的异常
java·开发语言
兮兮能吃能睡2 小时前
Python中的eval()函数详解
开发语言·python
returnShitBoy2 小时前
iOS 上的内存管理是如何处理的?
macos·ios·cocoa
狄加山6753 小时前
Qt模型-视图架构
开发语言·qt
Aphelios3803 小时前
TaskFlow开发日记 #1 - 原生JS实现智能Todo组件
java·开发语言·前端·javascript·ecmascript·todo
odoo中国3 小时前
Python 深度学习实战 第1章 什么是深度学习&代码示例
开发语言·python·深度学习
pumpkin845143 小时前
Rust 是如何层层防错的
开发语言·rust