UIImageView類別

weixin_34185364發表於2017-08-19
#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

相關文章