KWMImageUtil.m 7.88 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7
//
// Created by Yaotian on 2/7/14.
// Copyright (c) 2014 Kollway Mobile. All rights reserved.
//

#import "KWMImageUtil.h"
#import "KWMStringUtil.h"
houweibin committed
8
#import "Buy/Buy.h"
houweibin committed
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


@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)];
}

houweibin committed
128 129 130 131
+(NSArray *)getOriginalImages1:(BUYProduct *)product{
    NSMutableArray *productImages = [[NSMutableArray alloc]init];
    if(!product){
        return productImages;
houweibin committed
132
    }
houweibin committed
133 134 135 136 137
    NSArray *imageLinks = product.imagesArray;
    if(!imageLinks || imageLinks.count == 0){
        return productImages;
    }
    for(BUYImageLink *imageLink in imageLinks){
138
        NSString *productImage = imageLink.sourceURL.absoluteString;
houweibin committed
139
        [productImages addObject:productImage];
houweibin committed
140
    }
houweibin committed
141
    return productImages;
houweibin committed
142 143
}

houweibin committed
144 145 146 147 148

+(NSArray *)getOriginalImages2:(KWMDataProduct *)product{
    NSMutableArray *productImages = [[NSMutableArray alloc]init];
    if(!product){
        return productImages;
houweibin committed
149
    }
houweibin committed
150 151 152
    NSArray *imageLinks = product.images;
    if(!imageLinks || imageLinks.count == 0){
        return productImages;
houweibin committed
153
    }
houweibin committed
154 155 156
    for(NSDictionary *imageLink in imageLinks){
        if(imageLink[@"src"]){
            NSString *originalImage = imageLink[@"src"];
157
            [productImages addObject:originalImage];
houweibin committed
158 159 160
        }
    }
    return productImages;
houweibin committed
161 162
}

houweibin committed
163 164 165 166 167

+(NSArray *)getImageArrayBySize:(id)product ImageSize:(NSInteger)ImageSize{
    NSArray *imageArray = nil;
    if([product isKindOfClass:[BUYProduct class]]){
        imageArray = [self getOriginalImages1:product];
houweibin committed
168
    }
houweibin committed
169 170 171 172 173 174
    if([product isKindOfClass:[KWMDataProduct class]]){
        imageArray = [self getOriginalImages2:product];
    }
    NSMutableArray *newImageArray = [[NSMutableArray alloc]init];
    if(imageArray && imageArray.count>0){
        for(NSString *imageUrl in imageArray){
175 176
            NSString *newImageUrl = [self getProductImageUrlByOriginalUrl:imageUrl ImageSize:ImageSize];
            [newImageArray addObject:newImageUrl];
houweibin committed
177 178 179 180 181
        }
    }
    return newImageArray;
}

182 183 184 185 186 187 188 189 190 191

+ (NSString *)getProductImageUrlByOriginalUrl:(NSString *)originalImgUrl ImageSize:(NSInteger)ImageSize{
    NSMutableString *newImageUrl = [[NSMutableString alloc]initWithString:@""];;
    //https://cdn.shopify.com/s/files/1/1089/5284/products/5413_1.jpg?v==1459708223 转换为
    //https://o42yton8r.qnssl.com/s/files/1/1089/5284/products/5413_1.jpg?imageView2/1/w/300/h/300/v==1459708223
    if(originalImgUrl){
        NSRange startRange = [originalImgUrl rangeOfString:@"?"];
        if (startRange.location != NSNotFound) {
            newImageUrl=[[NSMutableString alloc]initWithString:originalImgUrl];
            NSArray<NSString *> *imageWH = [self getImageWH:ImageSize];
192
            NSString *size = [NSString stringWithFormat:@"imageView2/2/w/%@/h/%@/",imageWH[0],imageWH[1]];
193 194 195
            [newImageUrl insertString:size atIndex:startRange.location+1];
        }
    }
196 197
    return [newImageUrl stringByReplacingOccurrencesOfString:@"cdn.shopify.com" withString:Image_Domain];

198 199
}

houweibin committed
200
+(NSArray *)getImageWH:(NSInteger)ImageSize{
201 202 203 204 205 206 207 208 209 210 211
    switch (ImageSize) {
        case BigImage:
            return @[@"1000", @"1000"];
        case SmallImage:
            return @[@"300", @"300"];
        case LowImage:
            return @[@"150", @"150"];
        case NormalImage:
            return @[@"500", @"500"];
        default:
            return @[@"300", @"300"];
houweibin committed
212 213 214
    }
}

houweibin committed
215 216 217 218
+ (NSArray *)getProductImageUrls:(id)product ImageSize:(NSInteger)ImageSize{
    NSArray *imageUrls = nil;
    if(product){
        imageUrls = [self getImageArrayBySize:product ImageSize:ImageSize];
219
        return imageUrls;
houweibin committed
220 221 222 223 224
    }
    return nil;
}

+ (NSString *)getProductImageUrl:(id)product ImageSize:(NSInteger)ImageSize{
houweibin committed
225
    NSString *imageUrl = @"";
houweibin committed
226 227 228 229 230
    if(product){
        NSArray *imageArray = [self getImageArrayBySize:product ImageSize:ImageSize];
        if(imageArray && imageArray.count>0){
            imageUrl = imageArray.firstObject;
        }
houweibin committed
231 232 233 234
    }
    return imageUrl;
}

235

houweibin committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249
+ (NSString *)getProductImageUrlByPosition:(id)product ImageSize:(NSInteger)ImageSize Position:(NSInteger)Position{
    NSString *imageUrl = @"";
    if(product){
        NSArray *imageArray = [self getImageArrayBySize:product ImageSize:ImageSize];
        if(imageArray && imageArray.count>Position){
            return imageArray[Position];
        }
    }
    return imageUrl;
}




houweibin committed
250 251

@end