NSURL *imgUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",serverUrl,pic]]; //从服务器拿到图片的地址
// NSLog(@"fsdf %@",imgUrl);
if (hasCachedImage(imgUrl)) {
imghead.image = [UIImage imageWithContentsOfFile:pathForURL(imgUrl)]; //看本地理由木有这张图片的缓存
}else
{
NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:imgUrl,@"url",imghead,@"imageView",nil];
[NSThread detachNewThreadSelector:@selector(cacheImage:) toTarget:[ImageCacher defaultCacher] withObject:dic];
}
-(void)cacheImage:(NSDictionary*)aDic
{
NSURL *aURL=[aDic objectForKey:@"url"];
NSFileManager *fileManager=[NSFileManager defaultManager];
NSData *data=[NSData dataWithContentsOfURL:aURL] ;
UIImage *image=[UIImage imageWithData:data];
if (image==nil) {
return;
}
NSData *smallData=UIImagePNGRepresentation(image);
NSLog(@"%@",pathInCacheDirectory(@"com.xmly"));
NSString *pathStr = [NSString stringWithFormat:@"%@",pathInCacheDirectory(@"com.xmly")];
if (![fileManager fileExistsAtPath:pathStr]) {
[fileManager createDirectoryAtPath:pathStr withIntermediateDirectories:YES attributes:Nil error:Nil];
}
if (smallData) {
[fileManager createFileAtPath:pathForURL(aURL) contents:smallData attributes:nil];
}
UIButton *bt = [aDic objectForKey:@"uibtton"];
if (bt) {
CATransition *transtion = [CATransition animation];
transtion.duration = 0.5;
[transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[transtion setType:_type];
[transtion setSubtype:kCATransitionFromRight];
[bt.layer addAnimation:transtion forKey:@"transtionKey"];
[bt setImage:image forState:UIControlStateNormal];
UIGraphicsEndImageContext();
return;
}
UIView *view=[aDic objectForKey:@"imageView"];
//判断view是否还存在 如果tablecell已经移出屏幕会被回收 那么什么都不用做,下次滚到该cell 缓存已存在 不需要执行此方法
if (view!=nil) {
CATransition *transtion = [CATransition animation];
transtion.duration = 0.5;
[transtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[transtion setType:_type];
[transtion setSubtype:kCATransitionFromRight];
[view.layer addAnimation:transtion forKey:@"transtionKey"];
if ([[view class] isSubclassOfClass:[UIImageView class]]) {
if ([[aDic objectForKey:@"type"] boolValue]) {
view.backgroundColor = [UIColor clearColor];
[(UIImageView*)view setImage:[image grayscale:1]];
}
else
{
view.backgroundColor = [UIColor clearColor];
[(UIImageView*)view setImage:image];
}
}
else
{
if ([aDic objectForKey:@"mohu1"] && [[aDic objectForKey:@"mohu1"] length] > 0) {
view.layer.contents = (id)[image blurredImage:70].CGImage;
view.contentMode = UIViewContentModeScaleToFill;
}
else if ([aDic objectForKey:@"mohu"] && [[aDic objectForKey:@"mohu"] length] > 0) {
view.layer.contents = (id)[image blurredImage:70].CGImage;
view.contentMode = UIViewContentModeScaleAspectFill;
}
else if ([aDic objectForKey:@"fill"] && [[aDic objectForKey:@"fill"] length] > 0) {
view.layer.contents = (id)image.CGImage;
view.contentMode = UIViewContentModeScaleAspectFill;
}
else
{
if ([[aDic objectForKey:@"type"] boolValue]) {
view.backgroundColor = [UIColor colorWithPatternImage:[image grayscale:1]];
}
else
{
view.backgroundColor = [UIColor colorWithPatternImage:image];
}
}
}
}
UIGraphicsEndImageContext();
}