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];
        }
    });
}
相关推荐
编啊编程啊程3 小时前
JUC之AQS
java·开发语言·jvm·c++·kafka
好学且牛逼的马6 小时前
GOLANG 接口
开发语言·golang
ahauedu6 小时前
AI资深 Java 研发专家系统解析Java 中常见的 Queue实现类
java·开发语言·中间件
韭菜钟6 小时前
在Qt中用cmake实现类似pri文件的功能
开发语言·qt·系统架构
闲人编程6 小时前
Python第三方库IPFS-API使用详解:构建去中心化应用的完整指南
开发语言·python·去中心化·内存·寻址·存储·ipfs
CTRA王大大7 小时前
【golang】制作linux环境+golang的Dockerfile | 如何下载golang镜像源
linux·开发语言·docker·golang
2501_915106328 小时前
iOS混淆工具实战 金融支付类 App 的安全防护与合规落地
android·ios·小程序·https·uni-app·iphone·webview
I烟雨云渊T8 小时前
iOS 数据持久化
macos·ios·cocoa
zhangfeng11338 小时前
以下是基于图论的归一化切割(Normalized Cut)图像分割工具的完整实现,结合Tkinter界面设计及Python代码示
开发语言·python·图论
还梦呦9 小时前
2025年09月计算机二级Java选择题每日一练——第五期
java·开发语言·计算机二级