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];
        }
    });
}

猜你喜欢

转载自blog.csdn.net/LIUXIAOXIAOBO/article/details/132677796