// // AsyncImageView.m // tourpan // // Created by scar on 12-9-9. // Copyright 2012 __MyCompanyName__. All rights reserved. // #import "AsyncImageView.h" #import "Remote.h" #import #import "CoreAnimationEffect.h" #import "UIImage+ImageEffects.h" #import "UIImage+wiRoundedRectImage.h" #import "YFGIFImageView.h" #import "UIImageView+WebCache.h" @interface AsyncImageView() { } @property(nonatomic, readonly)NSString* imageName; @property(nonatomic, strong)UIImageView *waterPrint; @property(nonatomic, strong)YFGIFImageView *gifView; @property(nonatomic, strong)NSData *gifData; @end static UIImage* g_ImageBack = nil; @implementation AsyncImageView @synthesize contentMode; @synthesize imageUrl = _imageUrl; @synthesize imageView; @synthesize isHaveWaterPrint; @synthesize delegate; @synthesize isNeedReductionImage; @synthesize gifView = _gifView; @synthesize gifData = _gifData; @synthesize defaultImage = _defaultImage; @synthesize isNeedCorner = _isNeedCorner; - (void)setIsHaveWaterPrint:(BOOL)isWaterPrint { isHaveWaterPrint = isWaterPrint; if (!isWaterPrint) { [self.waterPrint removeFromSuperview]; } } - (void)setIsNeedCorner:(BOOL)NeedCorner { _isNeedCorner = NeedCorner; } - (id) initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor colorWithHexString:imageBackColor]; self.imageView = [[UIImageView alloc]initWithFrame:CGRectZero]; self.imageView.backgroundColor = [UIColor colorWithHexString:imageBackColor]; [self addSubview:imageView]; [self addSubview:self.gifView]; [self setupComponent]; [self addSubview:self.waterPrint]; self.isNeedReductionImage = NO; self.isHaveWaterPrint = YES; self.isNeedCorner = NO; self.isHighPriority = YES; //详情页:默认打底图尺寸不对 (bug:6053) self.clipsToBounds = YES; } return self; } - (void)setDefaultImage:(UIImage *)image { _defaultImage = image; } - (NSString*)imageName { NSString *imageID = getImageNameFromURL(self.imageUrl); NSString* imageName = imageID; NSString *imagePathName = CachePath(imageName); return imagePathName; } - (YFGIFImageView*)gifView { if (!_gifView) { _gifView = [[YFGIFImageView alloc]initWithFrame:CGRectZero]; _gifView.backgroundColor = [UIColor colorWithHexString:imageBackColor]; _gifView.hidden = YES; } return _gifView; } - (void) setupComponent { // Initialization code. self.contentMode = UIViewContentModeScaleToFill; self.backgroundColor = self.imageView.backgroundColor; self.clearsContextBeforeDrawing = YES; } - (void)setImageUrl:(NSString *)url { _imageUrl = url; if (isHaveWaterPrint) { self.waterPrint.hidden = NO; } if (!self.gifView.hidden) {// 新闻详情页:新闻详情页中的动态图加载好后,图中间还会出现“澎湃加载中”(bug;6103) self.gifView.hidden = YES; } if (!self.imageView.hidden) {// 新闻详情页:新闻详情页中的动态图加载好后,图中间还会出现“澎湃加载中”(bug;6103) self.imageView.hidden = YES; } if ([url isMatchedByRegex:@".gif"]) { if ([self.gifView isGIFPlaying]) { [self.gifView stopGIF]; } // self.gifView.hidden = YES; if (self.imageView.image) {// 新闻详情页:新闻详情页中的动态图加载好后,图中间还会出现“澎湃加载中”(bug;6103) self.imageView.image = nil; } self.imageView.hidden = NO; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData *data = [NSData dataWithContentsOfFile:self.imageName]; if (data) { self.gifData = data; }else { [self downLoadImage]; } }); }else { self.gifView.hidden = YES; self.imageView.hidden = NO; __weak typeof(self) Self = self; SDWebImageOptions options = SDWebImageHighPriority; if (!self.isHighPriority) { options = SDWebImageLowPriority; } [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:self.defaultImage options:options completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { if (image) { if (isHaveWaterPrint) { Self.waterPrint.hidden = YES; } if ([Self.delegate respondsToSelector:@selector(getImageSuccess:)]) { [Self.delegate getImageSuccess:image]; } }else { TPLOG(@"%@",error); if (Self.defaultImage) { if (isHaveWaterPrint) { Self.waterPrint.hidden = YES; } Self.imageView.image = self.defaultImage; [Self.imageView setNeedsDisplay]; } } }]; } } - (void)setGifData:(NSData *)data { dispatch_async(dispatch_get_main_queue(), ^{ [self updateGif:data]; }); } - (void)updateGif:(NSData*)data { // BOOL isAnimate = NO; self.gifView.hidden = NO; self.waterPrint.hidden = NO; self.imageView.hidden = YES; // if (!self.gifView.animatedImage) { // isAnimate = YES; // } // self.imagePrint.hidden = YES; // self.printLabel.hidden = YES; _gifData = data; self.gifView.gifPath = self.imageName; [self.gifView startGIF]; self.waterPrint.hidden = YES; if ([self.delegate respondsToSelector:@selector(getImageSuccess:)]) { [self.delegate getImageSuccess:nil]; } } - (void) layoutSubviews { self.imageView.frame = self.bounds; self.gifView.frame = self.bounds; if (isHaveWaterPrint) { self.waterPrint.frame = CGRectMake(CGRectGetWidth(self.imageView.bounds)/2-96/4, CGRectGetHeight(self.imageView.bounds)/2-99/4, 96/2, 99/2); } if (!self.isNeedCorner) { return; } self.imageView.layer.cornerRadius = CGRectGetWidth(self.bounds)/13; self.imageView.layer.borderWidth = 1; self.imageView.layer.borderColor = [UIColor colorWithHexString:@"0x646464"].CGColor; } - (UIImageView*)waterPrint { if (!_waterPrint) { _waterPrint = [[UIImageView alloc]initWithFrame:CGRectZero]; if ([[TPUserDefault instance].isNightMode intValue] == 1) { _waterPrint.image = Image(@"frontPage/imageBack_night.png"); }else{ _waterPrint.image = Image(@"frontPage/imageBack.png"); } } return _waterPrint; } - (void)downLoadImage { [Remote downloadFileAsync:self.imageUrl actionTag:1000+[getImageNumFromURL(self.imageUrl) intValue] filePath:self.imageName delegate:self]; } #pragma mark NetResponsDelegate - (void)startWaitCursor:(int)actionTag { } - (void)stopWaitCursor:(int)actionTag { } - (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; } } } - (void)dealloc { } @end