KWMImageUtil.m 8.21 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"
9
#import <RegexKitLite/RegexKitLite.h>
houweibin committed
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


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

houweibin committed
145 146 147 148 149

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

houweibin committed
164 165 166 167 168

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

183 184

+ (NSString *)getProductImageUrlByOriginalUrl:(NSString *)originalImgUrl ImageSize:(NSInteger)ImageSize{
185
    NSMutableString *newImageUrl = [[NSMutableString alloc]initWithString:[originalImgUrl stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"]];;
186 187
    //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
188 189 190 191 192 193 194 195 196 197 198 199 200
//    if(originalImgUrl){
//        NSRange startRange = [originalImgUrl rangeOfString:@"?"];
//        if (startRange.location != NSNotFound) {
//            newImageUrl=[[NSMutableString alloc]initWithString:originalImgUrl];
//            NSArray<NSString *> *imageWH = [self getImageWH:ImageSize];
//            NSString *size = [NSString stringWithFormat:@"imageView2/2/w/%@/h/%@/",imageWH[0],imageWH[1]];
//            [newImageUrl insertString:size atIndex:startRange.location+1];
//        }
//    }
    NSArray<NSString *> *imageWH = [self getImageWH:ImageSize];
    NSString *size = [NSString stringWithFormat:@"?imageView2/2/w/%@/h/%@/",imageWH[0],imageWH[1]];
    [newImageUrl appendString:size];
    
201 202
    return [newImageUrl stringByReplacingOccurrencesOfString:@"cdn.shopify.com" withString:Image_Domain];

203 204
}

houweibin committed
205
+(NSArray *)getImageWH:(NSInteger)ImageSize{
206 207 208 209 210 211 212 213 214 215 216
    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
217 218 219
    }
}

houweibin committed
220 221 222 223
+ (NSArray *)getProductImageUrls:(id)product ImageSize:(NSInteger)ImageSize{
    NSArray *imageUrls = nil;
    if(product){
        imageUrls = [self getImageArrayBySize:product ImageSize:ImageSize];
224
        return imageUrls;
houweibin committed
225 226 227 228 229
    }
    return nil;
}

+ (NSString *)getProductImageUrl:(id)product ImageSize:(NSInteger)ImageSize{
houweibin committed
230
    NSString *imageUrl = @"";
houweibin committed
231 232 233 234 235
    if(product){
        NSArray *imageArray = [self getImageArrayBySize:product ImageSize:ImageSize];
        if(imageArray && imageArray.count>0){
            imageUrl = imageArray.firstObject;
        }
houweibin committed
236 237 238 239
    }
    return imageUrl;
}

240

houweibin committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254
+ (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
255 256

@end