| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- //
- // GifImageView.m
- // iChatView
- //
- // Created by zzl on 16/6/3.
- // Copyright © 2016年 ldh. All rights reserved.
- //
- #import "GifImageView.h"
- #import "UIImageView+WebCache.h"
- @interface GifImageView ()
- {
- CGFloat _top;
- }
- @property (nonatomic, weak)IBOutlet FLAnimatedImageView *gifImage;
- @property (nonatomic, weak)IBOutlet UILabel *nickLabel;
- @property (nonatomic, assign) NSUInteger currentLoopIndex; // 当前循环的次数
- @property (nonatomic, copy) NSString *senderName;
- @end
- @implementation GifImageView
- - (id)initWithModel:(AnimateConfigModel*)gift inView:(UIView*)superView andSenderName:(NSString *)senderName
- {
- self = [[[NSBundle mainBundle] loadNibNamed:@"GifImageView" owner:self options:nil] lastObject];
- if (self)
- {
- self.backgroundColor = [UIColor clearColor];
- _nickLabel.hidden = YES;
- [superView addSubview:self];
- // [superView sendSubviewToBack:self];
-
- _senderName = senderName;
- if ([gift isKindOfClass:[NSDictionary class]]) {
- AnimateConfigModel *model = [AnimateConfigModel modelWithDictionary:gift];
-
- [self cfgWithGift:model andTop:0];
- }else{
- [self cfgWithGift:gift andTop:0];
- }
-
-
- }
- return self;
- }
- - (id)initWithModel2:(GiftModel *)gift inView:(UIView *)superView andSenderName:(NSString *)senderName
- {
- self = [[[NSBundle mainBundle] loadNibNamed:@"GifImageView" owner:self options:nil] lastObject];
- if (self)
- {
- self.backgroundColor = [UIColor clearColor];
- _nickLabel.hidden = YES;
- [superView addSubview:self];
-
- _senderName = senderName;
-
- [self cfgWithGift2:gift andTop:0];
- }
- return self;
- }
- - (void)cfgWithGift:(AnimateConfigModel*)gift andTop:(CGFloat)top
- {
- _giftItem = gift;
- _top = top;
-
- __weak typeof(self) ws = self;
-
- [self loadAnimatedImageWithURL:[NSURL URLWithString:gift.url] completion:^(FLAnimatedImage *animatedImage) {
- ws.gifImage.animatedImage = animatedImage;
- if (ws.giftItem.delay_time)
- {
- ws.hidden = YES;
- [ws performSelector:@selector(setUpView:) withObject:gift afterDelay:_giftItem.delay_time/1000];
- }
- else
- {
- [ws setUpView:gift];
- }
- }];
- }
- - (void)cfgWithGift2:(GiftModel*)gift andTop:(CGFloat)top
- {
- _giftModel = gift;
- _top = top;
-
- __weak typeof(self) ws = self;
-
- [self.gifImage sd_setImageWithURL:[NSURL URLWithString:gift.animated_url] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
-
- //图片下载完成 在这里进行相关操作,如加到数组里 或者显示在imageView上
- if (ws.giftItem.delay_time)
- {
- ws.hidden = YES;
- [ws performSelector:@selector(setUpView:) withObject:gift afterDelay:_giftItem.delay_time/1000];
- }
- else
- {
- [ws setUpView2:gift];
- }
-
- }];
- }
- - (void)setUpView2:(GiftModel*)gift
- {
- self.hidden = NO;
-
- [self setUserInteractionEnabled:NO];
-
- CGRect pRect = self.superview.frame; //superview的frame
- CGRect vRect = self.gifImage.frame; //gifImage的frame
- CGSize size = self.gifImage.currentFrame.size; //图片的size
- CGFloat imageScale = size.width/size.height; //图片宽高比
-
- CGFloat w = size.width;
- CGFloat h = size.height;
-
- /**
- gif_gift_show_style:
- 0:按像素显示模式(当某条边超出手机屏幕时该条边贴边),配合位置参数使用;
- 1、全屏显示模式(gif图片四个角顶住手机屏幕的四个角);
- 2、至少两条边贴边模式(按比例缩放到手机屏幕边界的最大尺寸),配合位置参数使用;
- */
- // if (gift.gif_gift_show_style == 0)
- // {
- // if ((size.width-pRect.size.width) > 0 && (size.height-pRect.size.height) > 0)
- // { //如果图片的宽、高都超过的屏幕宽、高
- // if ((size.width-pRect.size.width) > (size.height-pRect.size.height))
- // {
- // w = pRect.size.width;
- // h = w / imageScale;
- // }
- // else
- // {
- // h = pRect.size.height;
- // w = h * imageScale;
- // }
- // }
- // else if ((size.width-pRect.size.width) > 0 && (size.height-pRect.size.height) < 0)
- // { //如果图片的宽超过的屏幕宽,但是高小于屏幕的高
- // w = pRect.size.width;
- // h = w / imageScale;
- // }
- // else if ((size.width-pRect.size.width) < 0 && (size.height-pRect.size.height) > 0)
- // { //如果图片的高超过的屏幕高,但是宽小于屏幕的宽
- // h = pRect.size.height;
- // w = h * imageScale;
- // }
- // vRect.size = CGSizeMake(w, h);
- // self.gifImage.frame = vRect;
- // size = vRect.size;
- // }
- // else if (gift.gif_gift_show_style == 1)
- // {
- if (_giftItem.show_user == 0)
- {
- _nickLabel.hidden = YES;
- _nickLabel.text = @"";
- vRect = CGRectMake(0, 0, pRect.size.width, pRect.size.height);
- }
- else
- {
- _nickLabel.hidden = NO;
- vRect.size = CGSizeMake(pRect.size.width, pRect.size.height-_nickLabel.frame.size.height);
- }
- _nickLabel.hidden = YES;
- self.gifImage.frame = vRect;
- size = vRect.size;
- // }
- // else if (gift.gif_gift_show_style == 2)
- // {
- // if ((size.width-pRect.size.width) > 0 && (size.height-pRect.size.height) > 0)
- // { //如果图片的宽、高都超过的屏幕宽、高
- // if ((size.width-pRect.size.width) > (size.height-pRect.size.height))
- // {
- // w = pRect.size.width;
- // h = w / imageScale;
- // }
- // else
- // {
- // h = pRect.size.height;
- // w = h * imageScale;
- // }
- // }
- // else if ((size.width-pRect.size.width) > 0 && (size.height-pRect.size.height) < 0)
- // { //如果图片的宽超过的屏幕宽,但是高小于屏幕的高
- // w = pRect.size.width;
- // h = w / imageScale;
- // }
- // else if ((size.width-pRect.size.width) < 0 && (size.height-pRect.size.height) > 0)
- // { //如果图片的高超过的屏幕高,但是宽小于屏幕的宽
- // h = pRect.size.height;
- // w = h * imageScale;
- // }
- // else if ((size.width-pRect.size.width) < 0 && (size.height-pRect.size.height) < 0)
- // { //如果图片的宽、高都小于屏幕的宽
- // if ((size.width-pRect.size.width) > (size.height-pRect.size.height))
- // {
- // w = pRect.size.width;
- // h = w / imageScale;
- // }
- // else
- // {
- // h = pRect.size.height;
- // w = h * imageScale;
- // }
- // }
- //
- // vRect.size = CGSizeMake(w, h);
- // self.gifImage.frame = vRect;
- // size = vRect.size;
- // }
-
- // if (_giftItem.show_user == 0)
- // {
- // _nickLabel.hidden = YES;
- // _nickLabel.text = @"";
- // self.labelHeight.constant = 0;
- // }
- // else
- // {
- _nickLabel.hidden = NO;
- [self bringSubviewToFront:_nickLabel];
- _nickLabel.text = _senderName;
-
- if (size.height - _nickLabel.frame.size.height <= pRect.size.height)
- {
- size.height += _nickLabel.frame.size.height;
- }
- self.labelHeight.constant = 21;
- // }
- _nickLabel.hidden = YES;
- CGRect rect = CGRectMake((pRect.size.width-size.width)/2, _top, size.width, size.height);
-
- //0:使用path路径;1:屏幕上部;2:屏幕中间;3:屏幕底部
- // if (_giftItem.type == 0)
- // {
- // rect.origin.y = 0;
- // }
- // else if(_giftItem.type == 1)
- // {
- // rect.origin.y = 0;
- // }
- // else if(_giftItem.type == 2)
- // {
- rect.origin.y = (pRect.size.height - size.height)/2;
- // }
- // else if(_giftItem.type == 3)
- // {
- // rect.origin.y = pRect.size.height - size.height;
- // }
- self.frame = rect;
- [self layoutIfNeeded];
-
- __weak typeof(self) ws = self;
- self.gifImage.loopCompletionBlock = ^(NSUInteger loopCountRemaining){
-
- [ws dismissSelf];
- // ws.currentLoopIndex ++;
- //
- // if (ws.giftItem.play_count)
- // {
- // if (ws.giftItem.play_count == ws.currentLoopIndex)
- // {
- // [ws dismissSelf];
- // }
- // }
- // else
- // {
- // [ws performSelector:@selector(dismissSelf) withObject:gift afterDelay:_giftItem.duration/1000];
- // }
-
- };
- }
- - (void)setUpView:(AnimateConfigModel*)gift
- {
- self.hidden = NO;
-
- [self setUserInteractionEnabled:NO];
-
- CGRect pRect = self.superview.frame; //superview的frame
- CGRect vRect = self.gifImage.frame; //gifImage的frame
- CGSize size = self.gifImage.currentFrame.size; //图片的size
- CGFloat imageScale = size.width/size.height; //图片宽高比
-
- CGFloat w = size.width;
- CGFloat h = size.height;
-
- /**
- gif_gift_show_style:
- 0:按像素显示模式(当某条边超出手机屏幕时该条边贴边),配合位置参数使用;
- 1、全屏显示模式(gif图片四个角顶住手机屏幕的四个角);
- 2、至少两条边贴边模式(按比例缩放到手机屏幕边界的最大尺寸),配合位置参数使用;
- */
- if (gift.gif_gift_show_style == 0)
- {
- if ((size.width-pRect.size.width) > 0 && (size.height-pRect.size.height) > 0)
- { //如果图片的宽、高都超过的屏幕宽、高
- if ((size.width-pRect.size.width) > (size.height-pRect.size.height))
- {
- w = pRect.size.width;
- h = w / imageScale;
- }
- else
- {
- h = pRect.size.height;
- w = h * imageScale;
- }
- }
- else if ((size.width-pRect.size.width) > 0 && (size.height-pRect.size.height) < 0)
- { //如果图片的宽超过的屏幕宽,但是高小于屏幕的高
- w = pRect.size.width;
- h = w / imageScale;
- }
- else if ((size.width-pRect.size.width) < 0 && (size.height-pRect.size.height) > 0)
- { //如果图片的高超过的屏幕高,但是宽小于屏幕的宽
- h = pRect.size.height;
- w = h * imageScale;
- }
- vRect.size = CGSizeMake(w, h);
- self.gifImage.frame = vRect;
- size = vRect.size;
- }
- else if (gift.gif_gift_show_style == 1)
- {
- if (_giftItem.show_user == 0)
- {
- _nickLabel.hidden = YES;
- _nickLabel.text = @"";
- vRect = CGRectMake(0, 0, pRect.size.width, pRect.size.height);
- }
- else
- {
- _nickLabel.hidden = NO;
- vRect.size = CGSizeMake(pRect.size.width, pRect.size.height-_nickLabel.frame.size.height);
- }
-
- self.gifImage.frame = vRect;
- size = vRect.size;
- }
- else if (gift.gif_gift_show_style == 2)
- {
- if ((size.width-pRect.size.width) > 0 && (size.height-pRect.size.height) > 0)
- { //如果图片的宽、高都超过的屏幕宽、高
- if ((size.width-pRect.size.width) > (size.height-pRect.size.height))
- {
- w = pRect.size.width;
- h = w / imageScale;
- }
- else
- {
- h = pRect.size.height;
- w = h * imageScale;
- }
- }
- else if ((size.width-pRect.size.width) > 0 && (size.height-pRect.size.height) < 0)
- { //如果图片的宽超过的屏幕宽,但是高小于屏幕的高
- w = pRect.size.width;
- h = w / imageScale;
- }
- else if ((size.width-pRect.size.width) < 0 && (size.height-pRect.size.height) > 0)
- { //如果图片的高超过的屏幕高,但是宽小于屏幕的宽
- h = pRect.size.height;
- w = h * imageScale;
- }
- else if ((size.width-pRect.size.width) < 0 && (size.height-pRect.size.height) < 0)
- { //如果图片的宽、高都小于屏幕的宽
- if ((size.width-pRect.size.width) > (size.height-pRect.size.height))
- {
- w = pRect.size.width;
- h = w / imageScale;
- }
- else
- {
- h = pRect.size.height;
- w = h * imageScale;
- }
- }
-
- vRect.size = CGSizeMake(w, h);
- self.gifImage.frame = vRect;
- size = vRect.size;
- }
-
- if (_giftItem.show_user == 0)
- {
- _nickLabel.hidden = YES;
- _nickLabel.text = @"";
- self.labelHeight.constant = 0;
- }
- else
- {
- _nickLabel.hidden = NO;
- [self bringSubviewToFront:_nickLabel];
- _nickLabel.text = _senderName;
-
- if (size.height - _nickLabel.frame.size.height <= pRect.size.height)
- {
- size.height += _nickLabel.frame.size.height;
- }
- self.labelHeight.constant = 21;
- }
-
- CGRect rect = CGRectMake((pRect.size.width-size.width)/2, _top, size.width, size.height);
-
- //0:使用path路径;1:屏幕上部;2:屏幕中间;3:屏幕底部
- if (_giftItem.type == 0)
- {
- rect.origin.y = 0;
- }
- else if(_giftItem.type == 1)
- {
- rect.origin.y = 0;
- }
- else if(_giftItem.type == 2)
- {
- rect.origin.y = (pRect.size.height - size.height)/2;
- }
- else if(_giftItem.type == 3)
- {
- rect.origin.y = pRect.size.height - size.height;
- }
- self.frame = rect;
- [self layoutIfNeeded];
-
- _nickLabel.hidden = YES;
-
- __weak typeof(self) ws = self;
- self.gifImage.loopCompletionBlock = ^(NSUInteger loopCountRemaining){
-
- ws.currentLoopIndex ++;
-
- if (ws.giftItem.play_count)
- {
- if (ws.giftItem.play_count == ws.currentLoopIndex)
- {
- [ws dismissSelf];
- }
- }
- else
- {
- [ws performSelector:@selector(dismissSelf) withObject:gift afterDelay:_giftItem.duration/1000];
- }
-
- };
- }
- - (void)dismissSelf
- {
- FWWeakify(self)
- [UIView animateWithDuration:0.01 animations:^{
- FWStrongify(self)
- self.alpha = 0.0;
- } completion:^(BOOL finished) {
-
- FWStrongify(self)
-
- if (_giftItem) {
-
- if (self.delegate && [self.delegate respondsToSelector:@selector(gifImageViewFinish:andSenderName:)])
- {
- self.giftItem.isFinishAnimate = YES;
- [self.delegate gifImageViewFinish:self.giftItem andSenderName:self.senderName];
- }
- [self removeFromSuperview];
- }else{
- if (self.delegate && [self.delegate respondsToSelector:@selector(gifImageViewFinish2:andSenderName:)])
- {
- self.giftItem.isFinishAnimate = YES;
- [self.delegate gifImageViewFinish2:self.giftModel andSenderName:self.senderName];
- }
- [self removeFromSuperview];
- }
-
- }];
- }
- - (void)loadAnimatedImageWithURL:(NSURL *const)url completion:(void (^)(FLAnimatedImage *animatedImage))completion
- {
- NSString *const filename = url.lastPathComponent;
- NSString *const diskPath = [NSHomeDirectory() stringByAppendingPathComponent:filename];
-
- NSData * __block animatedImageData = [[NSFileManager defaultManager] contentsAtPath:diskPath];
- FLAnimatedImage * __block animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:animatedImageData];
-
- if (animatedImage) {
- if (completion) {
- completion(animatedImage);
- }
- } else {
- [[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
- animatedImageData = data;
- animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:animatedImageData];
- if (animatedImage) {
- if (completion) {
- dispatch_async(dispatch_get_main_queue(), ^{
- completion(animatedImage);
- });
- }
- [data writeToFile:diskPath atomically:YES];
- }
- }] resume];
- }
- }
- @end
|