#import <UIKit/UIKit.h>
@interface UIImageView (Extension)
+ (UIImageView *)imageViewWithFrame:(CGRect)frame
image:(UIImage *)image
placeHoldImage:(UIImage *)placeHoldImage;
+ (UIImageView *)waterMarkView:(UIImage *)image;
//加载图片 是否开启wifi模式
- (void)loadImgWithUrl:(NSString *)url withDefaultImg:(UIImage *)defaultImg withWiFiMode:(BOOL)wifiMode withIsWifi:(BOOL)isWifi;
//加载小图片
- (void)loadTinitImgWithUrl:(NSString *)url withDefaultImg:(UIImage *)defaultImg withWiFiMode:(BOOL)wifiMode withIsWifi:(BOOL)isWifi;
@end
#import "UIImageView+Extension.h"
@implementation UIImageView (Extension)
+ (UIImageView *)imageViewWithFrame:(CGRect)frame
image:(UIImage *)image
placeHoldImage:(UIImage *)placeHoldImage;
{
UIImageView *imageView = [[UIImageView alloc] initWithImage:image highlightedImage:placeHoldImage];
imageView.frame = frame;
return imageView;
}
+ (UIImageView *)waterMarkView:(UIImage *)image
{
UIImageView *imageView = [[UIImageView alloc] initWithImage:image highlightedImage:nil];
imageView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
imageView.userInteractionEnabled = NO;
imageView.alpha = 0.5;
// imageView.backgroundColor = colorForRedNormal;
return imageView;
}
- (void)loadImgWithUrl:(NSString *)url withDefaultImg:(UIImage *)defaultImg withWiFiMode:(BOOL)wifiMode withIsWifi:(BOOL)isWifi
{
// wifiMode YES:打开 NO:关闭
NSURL *imgUrl =[NSURL URLWithString:url];
__weak typeof(self)weakSelf = self;
if (wifiMode)
{
if (isWifi)
{
[self sd_setImageWithURL:imgUrl placeholderImage:defaultImg completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (image != defaultImg && image != nil && weakSelf.width != 0) {
UIImage *newImage = [image scaledImageWithNewSize:CGSizeMake(weakSelf.width, weakSelf.width*image.size.height/image.size.width)];
weakSelf.image = [UIImage getSubImage:newImage mCGRect:CGRectMake(0, 0, weakSelf.width, weakSelf.height) centerBool:YES];
}
}];
}else
{
self.image = defaultImg;
}
}else
{
[self sd_setImageWithURL:imgUrl placeholderImage:defaultImg completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (image != defaultImg && image != nil && weakSelf.width != 0) {
UIImage *newImage = [image scaledImageWithNewSize:CGSizeMake(weakSelf.width, weakSelf.width*image.size.height/image.size.width)];
weakSelf.image = [UIImage getSubImage:newImage mCGRect:CGRectMake(0, 0, weakSelf.width, weakSelf.height) centerBool:YES];
}
}];
}
}
- (void)loadTinitImgWithUrl:(NSString *)url withDefaultImg:(UIImage *)defaultImg withWiFiMode:(BOOL)wifiMode withIsWifi:(BOOL)isWifi
{
//处理图片的逻辑 如果有缓存则直接显示 没有则去下载
//图片URL
NSURL *imgUrl =[NSURL URLWithString:url];
// 占位图片
UIImage *placeholder = defaultImg;
// 从内存\沙盒缓存中获得原图,
UIImage *originalImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:url];
if (originalImage) { // 如果内存\沙盒缓存有原图,那么就直接显示原图(不管现在是什么网络状态)
self.image = originalImage;
} else { // 内存\沙盒缓存没有原图
// wifiMode YES:打开 NO:关闭
if (wifiMode)
{
if (isWifi)
{
[self sd_setImageWithURL:imgUrl placeholderImage:placeholder];
}else
{
self.image = defaultImg;
}
}else
{
[self sd_setImageWithURL:imgUrl placeholderImage:placeholder];
}
}
}
@end
网友评论