• iOS 展示网络GIF 图片


    方案一 使用sd_webimage

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

    方案二 将网络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;
                }
            }
            //        });
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    - (void)downLoadImage {
        if (!self.defaultImage) {
            self.image = nil;
        }
        [Remote downloadFileAsync:self.imageUrl actionTag:1000+[getImageNumFromURL(self.imageUrl) intValue] filePath:self.imageName delegate:self];
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    - (void)downloadFileAsync:(NSString*)requestUrl
                    actionTag:(int)actionTag
                     filePath:(NSString*)filePath
                     delegate:(id)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];
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    - (void)downloadFinish:(NSDictionary *)userInfo {
        @synchronized(self) {
            @try {
                id 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];
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

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

    - (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;
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    使用 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];
            }
        });
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
  • 相关阅读:
    centos环境启动/重启java服务脚本优化
    如何管理销售团队?
    Vue 2 生命周期与 Vue 3 生命周期:介绍与差别对比
    OLED显示图片
    【Python】Python第三方库总览
    mySQL
    CopyOnWriteArrayList源码分析
    Python模糊匹配(fuzzywuzzy package)
    极简工作流「GitHub 热点速览」
    Redis从入门到精通(二)-进阶篇
  • 原文地址:https://blog.csdn.net/LIUXIAOXIAOBO/article/details/132677796