KWMImageUtil.m 7.88 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
//
// Created by Yaotian on 2/7/14.
// Copyright (c) 2014 Kollway Mobile. All rights reserved.
//

#import "KWMImageUtil.h"
#import "KWMStringUtil.h"


@implementation KWMImageUtil {
    
}

+ (UIImage *)createTransparentImage {
    UIImage *result = nil;
    
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
    result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return result;
}


+ (UIImage *)createColorImage:(UIColor *)color pixelSize:(CGSize)size {
    UIImage *result = nil;
    
    CGRect rect = CGRectMake(0, 0, 0, 0);
    rect.size = size;
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
    [color setFill];
    UIRectFill(rect); // Fill it with your color
    result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return result;
}

+ (UIImage *)createColorImage:(UIColor *)color {
    return [self createColorImage:color pixelSize:CGSizeMake(1, 1)];
}

+ (UIImage *)createGradientImage:(UIColor *)startColor endColor:(UIColor *)endColor size:(CGSize)size {
    UIImage *result = nil;
    
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    
    CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();
    
    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1.0 };
    CGFloat r0;
    CGFloat g0;
    CGFloat b0;
    CGFloat a0;
    CGFloat r1;
    CGFloat g1;
    CGFloat b1;
    CGFloat a1;
    [startColor getRed:&r0 green:&g0 blue:&b0 alpha:&a0];
    [endColor getRed:&r1 green:&g1 blue:&b1 alpha:&a1];
    CGFloat components[8] = {
        r0, g0, b0, a0,   // Start color
        r1, g1, b1, a1 }; // End color
    CGGradientRef gradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
    CGContextDrawLinearGradient(currentContext, gradient, CGPointMake(0, 0), CGPointMake(0, size.height), 0);
    
    result = UIGraphicsGetImageFromCurrentImageContext();
    CGGradientRelease(gradient);
    CGColorSpaceRelease(rgbColorspace);
    
    return result;
}

// 计算文件大小,单位KB
+ (float)fileSizeAtPath:(NSString*)filePath{
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize]/1024.0;
    }
    return 0;
}

+ (BOOL)saveJPGImageToFile:(NSString *)filePath image:(UIImage *)image {
    NSData *data = UIImageJPEGRepresentation(image, .8f);
    return [data writeToFile:filePath atomically:YES];
}

+ (BOOL)savePNGImageToFile:(NSString *)filePath image:(UIImage *)image {
    NSData *data = UIImagePNGRepresentation(image);
    return [data writeToFile:filePath atomically:YES];
}

+ (UIImage *)getImageFromFile:(NSString *)filePath {
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    return [UIImage imageWithData:data];
}

+ (void)deleteLocalImage:(NSString *)imagePath {
    if (imagePath == nil || imagePath.length <= 0) {
        return;
    }
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:imagePath]) {
        NSError *error;
        [fileManager removeItemAtPath:imagePath error:&error];
    }
}

+ (UIImage *)imageWithImage:(UIImage *)image scaledToPixelSize:(CGSize)newSize{
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

+ (UIImage *)imageAfterZoom:(UIImage *)image scale:(float)scale {
    float newWidth = image.size.width * scale;
    float newHeight = image.size.height * scale;
    CGSizeMake(newWidth, newHeight);
    return [self imageWithImage:image scaledToPixelSize:CGSizeMake(newWidth, newHeight)];
}

+ (NSArray *)checkImage:(NSArray *)imagesArray{
    NSMutableArray *images = [[NSMutableArray alloc] initWithCapacity:100];
    if(imagesArray == nil){
        return [images copy];
    }
    for(KWMImage *image in imagesArray){
        if(image!=nil && ![KWMStringUtil isEmpty:image.value]){
            if( [image.value rangeOfString:@"jpg" options:NSCaseInsensitiveSearch].location!=NSNotFound
               ||
               [image.value rangeOfString:@"jpeg" options:NSCaseInsensitiveSearch].location!=NSNotFound
               ||
               [image.value rangeOfString:@"png" options:NSCaseInsensitiveSearch].location!=NSNotFound
               ||
               [image.value rangeOfString:@"gif" options:NSCaseInsensitiveSearch].location!=NSNotFound
               ){
                [images addObject:image];
            }
        }
    }
    return [images copy];
}


+ (NSString *)getProductImageUrl:(NSNumber *)productId ImageSize:(NSInteger)ImageSize{
    if(productId == nil){
        return nil;
    }
    
    //https://og95hd6ay.qnssl.com/5411820230-1.jpg
    NSString *domain = Image_Domain;
    NSString *imageUrl = [NSString stringWithFormat:@"https://%@/%@-1.jpg",domain,productId.stringValue];
    NSLog(@"1-----%@", imageUrl);
    if(ImageSize == BigImage){
        return [self getImage:1080 height:1080 image:imageUrl];
    }else if(ImageSize == SmallImage){
        return [self getImage:300 height:300 image:imageUrl];
    }else{
        return [self getImage:500 height:500 image:imageUrl];
    }
}

//content:5411820230-1.jpg 商品详细获取图片的方法
+ (NSString *)getProductImageUrl2:(NSString *)content ImageSize:(NSInteger)ImageSize{
    if([KWMStringUtil isEmpty:content]){
        return nil;
    }
    
    //https://og95hd6ay.qnssl.com/5411820230-1.jpg
    NSString *domain = Image_Domain;
    NSString *imageUrl = [NSString stringWithFormat:@"https://%@/%@",domain,content];
    NSLog(@"2-----%@", imageUrl);
    if(ImageSize == BigImage){
        return [self getImage:1000 height:1000 image:imageUrl];
    }else if(ImageSize == SmallImage){
        return [self getImage:300 height:300 image:imageUrl];
    }else{
        return [self getImage:500 height:500 image:imageUrl];
    }
}

+ (NSString *)getProductImageUrl3:(NSNumber *)productId ImageSize:(NSInteger)ImageSize Position:(NSInteger)Position{
    if(productId == nil){
        return nil;
    }
    //https://og95hd6ay.qnssl.com/5411820230-1.jpg
    NSString *domain = Image_Domain;
    NSString *imageUrl = [NSString stringWithFormat:@"https://%@/%@-%ld.jpg",domain,productId.stringValue,(long)Position];
    NSLog(@"3-----%@", imageUrl);
    if(ImageSize == BigImage){
        return [self getImage:1080 height:1080 image:imageUrl];
    }else if(ImageSize == SmallImage){
        return [self getImage:300 height:300 image:imageUrl];
    }else{
        return [self getImage:500 height:500 image:imageUrl];
    }
}

+(NSString *)getImage:(NSInteger)width height:(NSInteger)height image:(NSString*)image{
    NSString *imageUrl = @"";
    
//    
//    NSCalendar *calendar = [NSCalendar currentCalendar];
//    NSDate *now = [NSDate date];
//    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
//    NSDate *startDate = [calendar dateFromComponents:components];
//    long long time = [startDate timeIntervalSince1970];
    
    if([KWMStringUtil isEmpty:image]){
        return imageUrl;
    }
    
    NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
    [dateformat setDateFormat:@"YYYY-MM-dd"];
    NSString *today = [dateformat stringFromDate:[NSDate date]];
    [dateformat setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSDate *date = [dateformat dateFromString:[NSString stringWithFormat:@"%@ 00:00:00",today]];
    NSTimeInterval interval = [date timeIntervalSince1970]/100;
    long long  time = interval;
    
    imageUrl = [[NSString alloc] initWithFormat:@"%@?imageView2/1/w/%ld/h/%ld&_=%llu",image,(long)width,(long)height,time];
    
    NSLog(@"---4--%@", imageUrl);

    return imageUrl;
}


@end