|
//
// AsyncImageView.m
// tourpan
//
// Created by scar on 12-9-9.
// Copyright 2012 __MyCompanyName__. All rights reserved.
//
#import "AsyncImageView.h"
#import "Remote.h"
#import <sys/xattr.h>
#import "CoreAnimationEffect.h"
#import "FLAnimatedImage.h"
#import "FLAnimatedImageView.h"
#import "SDWebImage/SDWebImageManager.h"
#import "UIImageView+WebCache.h"
@interface AsyncImageView()<RemoteDelegate> {
UIImage* image;
UIImage* defaultImage;
}
@property(nonatomic, strong)UIImage* image;
@property(nonatomic, readonly)NSString* imageName;
@property(nonatomic, strong)UIImageView *waterPrint;
@property(nonatomic, strong)FLAnimatedImageView *gifView;
@property(nonatomic, strong)NSData *gifData;
@end
static UIImage* g_ImageBack = nil;
@implementation AsyncImageView
@synthesize defaultImage;
@synthesize contentMode;
@synthesize imageTime;
@synthesize imageUrl;
@synthesize imageView;
@synthesize isHaveWaterPrint;
@synthesize request;
@synthesize gifView = _gifView;
@synthesize gifData = _gifData;
#define ActivityIndicatorViewTag - 1111
- (UIImage*) image {
return image;
}
- (void) setImage:(UIImage *)_image {
BOOL isAnimate = NO;
if (!self.imageView.image) {
isAnimate = YES;
}
dispatch_async(dispatch_get_main_queue(), ^{
if (_image) {
image = _image;
self.waterPrint.hidden = YES;
self.gifView.hidden = YES;
self.imageView.hidden = NO;
self.imageView.image = _image;
[self.imageView setNeedsDisplay];
if (!self.isHaveWaterPrint) {
self.backgroundColor = [UIColor clearColor];
}
if ([self.delegate respondsToSelector:@selector(loadImageSuccess)]) {
[self.delegate loadImageSuccess];
}
if (isAnimate) {
if ([[TPUserDefault instance].isIphone4 intValue] == 0) {
self.imageView.alpha = 0.0;
[UIView animateWithDuration:0.25
animations:^{
self.imageView.alpha = 1.0;
}];
}
}
}else {
if (self.isHaveWaterPrint) {
self.waterPrint.hidden = NO;
}else {
self.waterPrint.hidden = YES;
}
self.imageView.image = nil;
[self.imageView setNeedsDisplay];
if (self.backColor) {
self.backgroundColor = self.backColor;
}
}
});
}
- (void)setGifData:(NSData *)data {
BOOL isAnimate = NO;
if (!self.gifView.animatedImage) {
isAnimate = YES;
}
_gifData = data;
FLAnimatedImage *gifImage = [FLAnimatedImage animatedImageWithGIFData:data];
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.hidden = YES;
self.gifView.animatedImage = gifImage;
self.gifView.hidden = NO;
if (isAnimate) {
if ([[TPUserDefault instance].isIphone4 intValue] == 0) {
[CoreAnimationEffect animationEaseIn:self];
}
}
});
}
- (FLAnimatedImageView*)gifView {
if (!_gifView) {
_gifView = [[FLAnimatedImageView alloc]initWithFrame:CGRectZero];
_gifView.backgroundColor = [UIColor clearColor];
_gifView.hidden = YES;
}
return _gifView;
}
- (void)setIsHaveWaterPrint:(BOOL)isWaterPrint {
isHaveWaterPrint = isWaterPrint;
if (!isWaterPrint) {
self.waterPrint.hidden = YES;
}
}
- (NSString*)imageName {
NSString* date = StringFromDate(self.imageTime, @"yyyy-MM-dd HH:mm:ss");
if (date.length <= 0) {
date = @"";
}
NSString* imageName = [date stringByAppendingFormat:@"%@", _imageId];
// imageName = [imageName md5];
NSString *imagePathName = CachePath(imageName);
return imagePathName;
}
- (void) setDefaultImage:(UIImage*)_image {
defaultImage = _image;
// self.image = _image;
}
- (NSString*)getFullPath:(NSString*)fileName {
if ([[fileName pathExtension] length] > 0) {
return ConfigPath([fileName lastPathComponent]);
} else {
return nil;
}
}
- (id) initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor colorWithHexString:imageBackColor];
self.imageView = [[UIImageView alloc]initWithFrame:CGRectZero];
self.imageView.backgroundColor = [UIColor clearColor];
[self setupComponent];
[self addSubview:self.waterPrint];
[self addSubview:imageView];
/**
* bug:5910(倒退】话题精选页:下拉刷新会一瞬间出来“加载中”小方框)
*/
[self addSubview:self.gifView];
self.isHaveWaterPrint = YES;
self.clipsToBounds = YES;
[self.imageView makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
[self.gifView makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
[self.waterPrint makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.centerX);
make.centerY.equalTo(self.centerY);
make.size.mas_equalTo(CGSizeMake(96/2, 99/2));
}];
// self.imageView.frame = self.bounds;
// self.gifView.frame = self.bounds;
// self.waterPrint.frame = CGRectMake(CGRectGetWidth(self.imageView.bounds)/2-96/4, CGRectGetHeight(self.imageView.bounds)/2-99/4, 96/2, 99/2);
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshAfterOpenNightMode:) name:REFRESHAFTERNIGHTMODE object:nil];
}
return self;
}
- (void) setupComponent {
// Initialization code.
self.contentMode = UIViewContentModeScaleToFill;
self.backgroundColor = [UIColor clearColor];
self.clearsContextBeforeDrawing = YES;
}
- (void) setImageId:(NSString *)imageId {
if ([_imageId isEqualToString:imageId]) {
return;
}
_imageId = imageId;
if (isBlankString(imageId)) {
return;
}
self.imageView.hidden = NO;
[self.gifView setHidden:YES];
if (isHaveWaterPrint) {
self.waterPrint.hidden = NO;
}
if (imageId.length <= 0) {
self.image = nil;
return;
}
if ([self.imageUrl isMatchedByRegex:@".gif"]) {
self.imageView.hidden = YES;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 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;
}
}
});
}else {
self.gifView.hidden = YES;
self.imageView.hidden = NO;
if ([self.imageUrl hasPrefix:@"http"]) {
__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 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
if (Self.backColor) {
Self.backgroundColor = self.backColor;
}
/**
* bug:5638( 栏目导航页:加载完栏目logo,再次进入时会有一瞬间显示蓝底)
*/
}
completed:^(UIImage *img, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (img) {
if (!Self.isHaveWaterPrint) {
Self.backgroundColor = [UIColor clearColor];
}
if ([Self.delegate respondsToSelector:@selector(loadImageSuccess)]) {
[Self.delegate loadImageSuccess];
}
dispatch_async(dispatch_get_main_queue(), ^{
if (isHaveWaterPrint) {
Self.waterPrint.hidden = YES;
}
});
if (img && cacheType == SDImageCacheTypeNone)
{
Self.imageView.alpha = 0.0;
[UIView animateWithDuration:0.25
animations:^{
Self.imageView.alpha = 1.0;
}];
}
}else {
TPLOG(@"%@",error);
if (Self.backColor) {
Self.backgroundColor = self.backColor;
}
if (Self.defaultImage) {
if (Self.isHaveWaterPrint) {
Self.waterPrint.hidden = NO;
}else {
Self.waterPrint.hidden = YES;
}
Self.imageView.image = self.defaultImage;
[Self.imageView setNeedsDisplay];
}
}
}];
}else {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSString *imagePath = [NSString stringWithFormat:@"%@%@",[TPUserDefault instance].offlinePath,self.imageUrl];
UIImage* IMAGE = [UIImage imageWithContentsOfFile:imagePath];
if (IMAGE) {
self.image = IMAGE;
}else self.image = nil;
});
}
}
// dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
// if ([self.imageUrl hasPrefix:@"http"]) {
// if (ExistAtPath(self.imageName)) {
// if ([self.imageName isMatchedByRegex:@".gif"]) {
// NSData *data = [NSData dataWithContentsOfFile:self.imageName];
// if (data) {
// self.gifData = data;
// }else {
// [self downLoadImage];
// }
// }else {
// UIImage* IMAGE = [UIImage imageWithContentsOfFile:self.imageName];
// if (IMAGE) {
// self.image = IMAGE;
// }else {
// [self downLoadImage];
// }
// }
// }else {
// [self downLoadImage];
// }
// }else {
// NSString *imagePath = [NSString stringWithFormat:@"%@%@",[TPUserDefault instance].offlinePath,self.imageUrl];
// if (ExistAtPath(imagePath)) {
// if ([self.imageId isMatchedByRegex:@".gif"]) {
// NSData *data = [NSData dataWithContentsOfFile:imagePath];
// if (data) {
// self.gifData = data;
// }else self.gifData = nil;
// }else {
// UIImage* IMAGE = [UIImage imageWithContentsOfFile:imagePath];
// if (IMAGE) {
// self.image = IMAGE;
// }else self.image = nil;
// }
// }else self.image = 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)setImageUrl:(NSString *)url {
// if (!url) {
// _imageId= nil;
// imageUrl = nil;
// self.image = nil;
// return;
// }
//
// imageUrl = url;
//}
- (void) layoutSubviews {
[super layoutSubviews];
if (self.backColor && !self.imageView.image) {
}else {
if (self.isHaveWaterPrint) {
self.backgroundColor = [UIColor colorWithHexString:imageBackColor];
}else {
if (![self.backgroundColor isEqual:[UIColor clearColor]]) {
self.backgroundColor = [UIColor colorWithHexString:imageBackColor];
}else {
self.backgroundColor = [UIColor clearColor];
}
if (self.imageView.image) {
self.backgroundColor = [UIColor clearColor];
}
}
}
/**
* bug:5278(打底图问题汇总)
*/
}
- (void)refreshAfterOpenNightMode:(id)sender {
self.backgroundColor = [UIColor colorWithHexString:imageBackColor];
if ([[TPUserDefault instance].isNightMode intValue] == 1) {
self.waterPrint.image = Image(@"frontPage/imageBack_night.png");//dddddd
}else{
self.waterPrint.image = Image(@"frontPage/imageBack.png");//d9dcdc
}
}
- (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;
}
#pragma mark NetResponsDelegate
- (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;
}
}
}
- (void) remoteResponsFailed:(int)actionTag withMessage:(NSString*)message {
NSLog(@"下载图片超时!");
}
//NSString* StringFromDate(NSDate* aDate, NSString *aFormat) {
// NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// [formatter setTimeZone:[NSTimeZone defaultTimeZone]];
// [formatter setDateFormat:aFormat];
// NSString *dateString = [formatter stringFromDate:aDate];
//
// return dateString;
//}
//NSDate* DateFromString(NSString* string, NSString* aFormat) {
// NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// // [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai"]];
// [formatter setTimeZone:[NSTimeZone defaultTimeZone]];
// [formatter setDateFormat:aFormat];
// NSDate *date = [formatter dateFromString:string];
//
// return date;
//}
- (void)startWaitCursor:(int)actionTag {
}
- (void)stopWaitCursor:(int)actionTag {
}
//页面需要被销毁,断开网络请求
- (void) stopRemoteRequestWhenPop {
[self.request clearDelegatesAndCancel];
self.request.userInfo = nil;
self.request = nil;
}
- (void)dealloc {
[self stopRemoteRequestWhenPop];
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
|