YH_ImageSliceService.m 26.7 KB
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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
//
//  YH_ImageSliceService.m
//  Yohoboys
//
//  Created by redding on 14-10-9.
//  Copyright (c) 2014年 YOHO. All rights reserved.
//
// 七牛切图接口指南 http://developer.qiniu.com/docs/v6/api/reference/fop/image/imagemogr2.html

#import "YH_ImageSliceService.h"
#import "Macros.h"
//#import "YH_Global.h"
//kImageMogr2Template
static NSString * const kJpgMogr2Template = @"?imageMogr2/auto-orient/thumbnail/%@/gravity/North/crop/%ldx%ld";
static NSString * const kImageFixedHeightTemplate = @"x%ld";
static NSString * const kImageFixedWidthTemplate = @"%ldx";

static NSInteger const kScaleFor6p = 3;
static NSInteger const kScaleFor456  = 2;

@implementation YH_ImageSliceService

+ (NSURL *)sliceImageURL:(NSURL *)originURL sliceType:(YH_ImageURLSliceType)sliceType originSize:(CGSize)originSize toSize:(CGSize)toSize {
//    if (originURL != nil) {
//        NSString *originURLString = [originURL absoluteString];
//        if ([YH_ImageSliceService isHaveImageSlice:originURLString]) {
//            return originURL;
//        }
//        
//    }
//    NSString *sliceString = nil;
//    NSString *sizeString = nil;
//    CGFloat scale = [[UIScreen mainScreen] scale];
//    NSString *kImageMogr2Template = @"";
//    switch (sliceType) {
//        case YH_ImageURLSliceTypeList:{
//            //列表的图片在各分辨率下的屏幕尺寸:iPhone6以下 - 290 185; iPhone6 - 345 221; iPhone6 Plus - 384 245
//            //对应的图片实际大小           :iPhone6以下 - 580 370; iPhone6 - 690 442; iPhone6 Plus - 1152 735
//            kImageMogr2Template = kJpgMogr2Template;
//            if (originSize.width > originSize.height) {
//                if (IS_IPHONE_6P) {
//                    
//                    sizeString = [NSString stringWithFormat:kImageFixedHeightTemplate, (long)YH_ListImageSizeiPhone6PHeight * kScaleFor6p];
//                    sliceString = [NSString stringWithFormat:kImageMogr2Template, sizeString, (NSInteger)(YH_ListImageSizeiPhone6PWidth * kScaleFor6p), (NSInteger)(YH_ListImageSizeiPhone6PHeight * kScaleFor6p)];
//                } else if (IS_IPHONE_6) {
//                    sizeString = [NSString stringWithFormat:kImageFixedHeightTemplate, (long)YH_ListImageSizeiPhone6Height * kScaleFor456];
//                    sliceString = [NSString stringWithFormat:kImageMogr2Template, sizeString, (NSInteger)(YH_ListImageSizeiPhone6Width * kScaleFor456), (NSInteger)(YH_ListImageSizeiPhone6Height * kScaleFor456)];
//                } else {
//                    sizeString = [NSString stringWithFormat:kImageFixedHeightTemplate, (long)YH_ListImageSizeBeforeiPhone6Height * kScaleFor456];
//                    sliceString = [NSString stringWithFormat:kImageMogr2Template, sizeString, (NSInteger)(YH_ListImageSizeBeforeiPhone6Width * kScaleFor456), (NSInteger)(YH_ListImageSizeBeforeiPhone6Height * kScaleFor456)];
//                }
//            } else {
//                if (IS_IPHONE_6P) {
//                    sizeString = [NSString stringWithFormat:kImageFixedWidthTemplate, (long)YH_ListImageSizeiPhone6PWidth * kScaleFor6p];
//                    sliceString = [NSString stringWithFormat:kImageMogr2Template, sizeString, (NSInteger)(YH_ListImageSizeiPhone6PWidth * kScaleFor6p), (NSInteger)(YH_ListImageSizeiPhone6PHeight * kScaleFor6p)];
//                } else if (IS_IPHONE_6) {
//                    sizeString = [NSString stringWithFormat:kImageFixedWidthTemplate, (long)YH_ListImageSizeiPhone6Width * kScaleFor456];
//                    sliceString = [NSString stringWithFormat:kImageMogr2Template, sizeString, (NSInteger)(YH_ListImageSizeiPhone6Width * kScaleFor456), (NSInteger)(YH_ListImageSizeiPhone6Height * kScaleFor456)];
//                } else {
//                    sizeString = [NSString stringWithFormat:kImageFixedWidthTemplate, (long)YH_ListImageSizeBeforeiPhone6Width * kScaleFor456];
//                    sliceString = [NSString stringWithFormat:kImageMogr2Template, sizeString, (NSInteger)(YH_ListImageSizeBeforeiPhone6Width * kScaleFor456), (NSInteger)(YH_ListImageSizeBeforeiPhone6Height * kScaleFor456)];
//                }
//            }
//            
//            
//        }
//            break;
//        case YH_ImageURLSliceTypeRuleDetail:{
//            kImageMogr2Template = @"?imageMogr2/thumbnail/%@/gravity/North/crop/%ldx";
//            if (originSize.width > originSize.height) {
//                
//                sizeString = [NSString stringWithFormat:@"%ldx",(long)(SCREEN_WIDTH * scale)];
//                
//            } else {
//                
//                sizeString = [NSString stringWithFormat:@"x%ld",(long)(SCREEN_HEIGHT * scale)];
//                
//            }
//            
//            sliceString = [NSString stringWithFormat:kImageMogr2Template, sizeString, (long)(SCREEN_WIDTH * scale)];
//        }
//            break;
//            
//        case YH_ImageURLSliceTypeDetailTop:{
//            kImageMogr2Template = @"?imageView2/2/w/%ld/h/%ld";
//            
//            CGFloat width = SCREEN_WIDTH;
//            CGFloat height = width * ((CGFloat)YH_ContentImageSizeBeforeiPhone6Height / YH_ContentImageSizeBeforeiPhone6Width);
//            sliceString = [NSString stringWithFormat:kImageMogr2Template, (long)(width * scale), (long)(height * scale)];
//
//        }
//            break;
//            
//        case YH_ImageURLSliceTypeRelatedPost:{
//            if (originSize.width > originSize.height) {
//                if (IS_IPHONE_6P) {
//                    sizeString = [NSString stringWithFormat:kImageFixedHeightTemplate, (long)YH_RelatedPostImageSizeHeight * 3];
//                } else {
//                    sizeString = [NSString stringWithFormat:kImageFixedHeightTemplate, (long)YH_RelatedPostImageSizeHeight * 2];
//                }
//            } else {
//                if (IS_IPHONE_6P) {
//                    sizeString = [NSString stringWithFormat:kImageFixedWidthTemplate, (long)YH_RelatedPostImageSizeWidth * 3];
//                } else {
//                    sizeString = [NSString stringWithFormat:kImageFixedWidthTemplate, (long)YH_RelatedPostImageSizeWidth * 2];
//                }
//            }
//            
//            sliceString = [NSString stringWithFormat:kJpgMogr2Template, sizeString, (long)(YH_RelatedPostImageSizeWidth * scale), (long)(YH_RelatedPostImageSizeHeight * scale)];
//
//        }
//            break;
//            
//        case YH_ImageURLSliceTypeCalendar:{
//            sliceString = [NSString stringWithFormat:@"?imageMogr2"];
//            
//            if (originSize.width > originSize.height) {
//                sliceString = [sliceString stringByAppendingFormat:@"/thumbnail/x%d/gravity/North/crop/%dx%d", (int)(toSize.height * scale),(int)(toSize.width * scale), (int)(toSize.height * scale)];
//            } else {
//                sliceString = [sliceString stringByAppendingFormat:@"/thumbnail/%dx/gravity/North/crop/%dx%d", (int)(toSize.width * scale),(int)(toSize.width * scale), (int)(toSize.height * scale)];
//            }
//
//            
//        }
//            break;
//            
//        default:
//            break;
//    }
//    
//    /*
//     如果是gif图片,转换成gif格式
//     否则转换为jpg格式
//     */
//    //WWAN情况下,渐进显示图片
//    if ([[AFNetworkReachabilityManager sharedManager] isReachableViaWWAN]) {
//        sliceString = [sliceString stringByAppendingString:@"/interlace/1"];
//        
//    }
//    
//    if ([YH_ImageSliceService isJPGImage:[originURL absoluteString]]) {
//        sliceString = [YH_ImageSliceService transformImageURL:sliceString];
//    }
//    return [[NSURL URLWithString:sliceString relativeToURL:originURL] absoluteURL];
    return nil;
}

//列表的图片在各分辨率下的屏幕尺寸:iPhone6以下 - 290 185; iPhone6 - 345 221; iPhone6 Plus - 384 245
//对应的图片实际大小           :iPhone6以下 - 580 370; iPhone6 - 690 442; iPhone6 Plus - 1152 735
//+ (NSURL *)listImageSliceSize:(CGSize)size originURL:(NSURL *)originURL
//{
//    //    CGFloat scale = [[UIScreen mainScreen] scale];
//    NSString *sliceString;
//    NSString *sizeString;
//    NSString *kImageMogr2Template = @"";
//    NSString *imageExt = [originURL.pathExtension lowercaseString];
//    
//    
//    static NSString * const kPNGMogr2Template = @"?imageMogr2/thumbnail/%@/gravity/North/crop/%dx%d/format/png";
//    
//    if ([imageExt isEqualToString:@"png"]) {
//        kImageMogr2Template = kPNGMogr2Template;
//    } else if([imageExt isEqualToString:@"jpg"]) {
//#ifdef SD_WEBP
//        kImageMogr2Template = kJpgMogr2Template;
//#else
//        kImageMogr2Template = @"?imageMogr2/thumbnail/%@/gravity/North/crop/%dx%d/format/jpg";
//#endif
//    } else {
//        kImageMogr2Template = kGifMogr2Template;
//    }
//    if (size.width > size.height) {
//        if (IS_IPHONE_6P) {
//            
//            sizeString = [NSString stringWithFormat:kImageFixedHeightTemplate, YH_ListImageSizeiPhone6PHeight * kScaleFor6p];
//            sliceString = [NSString stringWithFormat:kImageMogr2Template, sizeString, (NSInteger)(YH_ListImageSizeiPhone6PWidth * kScaleFor6p), (NSInteger)(YH_ListImageSizeiPhone6PHeight * kScaleFor6p)];
//        } else if (IS_IPHONE_6) {
//            sizeString = [NSString stringWithFormat:kImageFixedHeightTemplate, YH_ListImageSizeiPhone6Height * kScaleFor456];
//            sliceString = [NSString stringWithFormat:kImageMogr2Template, sizeString, (NSInteger)(YH_ListImageSizeiPhone6Width * kScaleFor456), (NSInteger)(YH_ListImageSizeiPhone6Height * kScaleFor456)];
//        } else {
//            sizeString = [NSString stringWithFormat:kImageFixedHeightTemplate, YH_ListImageSizeBeforeiPhone6Height * kScaleFor456];
//            sliceString = [NSString stringWithFormat:kImageMogr2Template, sizeString, (NSInteger)(YH_ListImageSizeBeforeiPhone6Width * kScaleFor456), (NSInteger)(YH_ListImageSizeBeforeiPhone6Height * kScaleFor456)];
//        }
//    } else {
//        if (IS_IPHONE_6P) {
//            sizeString = [NSString stringWithFormat:kImageFixedWidthTemplate, YH_ListImageSizeiPhone6PWidth * kScaleFor6p];
//            sliceString = [NSString stringWithFormat:kImageMogr2Template, sizeString, (NSInteger)(YH_ListImageSizeiPhone6PWidth * kScaleFor6p), (NSInteger)(YH_ListImageSizeiPhone6PHeight * kScaleFor6p)];
//        } else if (IS_IPHONE_6) {
//            sizeString = [NSString stringWithFormat:kImageFixedWidthTemplate, YH_ListImageSizeiPhone6Width * kScaleFor456];
//            sliceString = [NSString stringWithFormat:kImageMogr2Template, sizeString, (NSInteger)(YH_ListImageSizeiPhone6Width * kScaleFor456), (NSInteger)(YH_ListImageSizeiPhone6Height * kScaleFor456)];
//        } else {
//            sizeString = [NSString stringWithFormat:kImageFixedWidthTemplate, YH_ListImageSizeBeforeiPhone6Width * kScaleFor456];
//            sliceString = [NSString stringWithFormat:kImageMogr2Template, sizeString, (NSInteger)(YH_ListImageSizeBeforeiPhone6Width * kScaleFor456), (NSInteger)(YH_ListImageSizeBeforeiPhone6Height * kScaleFor456)];
//        }
//    }
//    
//    //WWAN情况下,渐进显示图片
//    if ([[AFNetworkReachabilityManager sharedManager] isReachableViaWWAN]) {
//        sliceString = [sliceString stringByAppendingString:@"/interlace/1"];
//    }
//    
//    NSLog(@"list size is %@", NSStringFromCGSize(size));
//    NSLog(@"list string is %@", [[NSURL URLWithString:sliceString relativeToURL:originURL] absoluteURL]);
//    
//    return [[NSURL URLWithString:sliceString relativeToURL:originURL] absoluteURL];
//}

//+ (NSURL *)detailTopImageSlice:(NSURL *)originURL
//{
//    CGFloat scale = [[UIScreen mainScreen] scale];
//    CGFloat width = SCREEN_WIDTH;
//    CGFloat height = width * ((CGFloat)YH_ContentImageSizeBeforeiPhone6Height / YH_ContentImageSizeBeforeiPhone6Width);
//    NSString *sliceString = [NSString stringWithFormat:@"?imageView2/2/w/%ld/h/%ld", (long)(width * scale), (long)(height * scale)];
//    NSString *imageExt = [originURL.pathExtension lowercaseString];
//    if ([imageExt isEqualToString:@"png"] || [imageExt isEqualToString:@"bmp"]) {
//        sliceString = [sliceString stringByAppendingString:@"/format/png"];
//    } else if ([imageExt isEqualToString:@"jpg"]) {
//#ifdef SD_WEBP
//        sliceString = [sliceString stringByAppendingString:@"/format/webp"];
//#else
//        sliceString = [sliceString stringByAppendingString:@"/format/jpg"];
//#endif
//    }
//    
//    NSString *URLString = [[originURL absoluteString] stringByAppendingString:sliceString];
//    NSLog(@"detail top image url is %@", [NSURL URLWithString:[URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]);
//    return [NSURL URLWithString:[URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
////    return [[NSURL URLWithString:sliceString relativeToURL:originURL] absoluteURL];
//}


//列表的图片在各分辨率下的屏幕尺寸:iPhone6以下 - 96 61; iPhone6 - 96 61; iPhone6 Plus - 96 61
//对应的图片实际大小           :iPhone6以下 - 192 122; iPhone6 - 192 122; iPhone6 Plus - 288 183
//+ (NSURL *)relatedPostImageSliceSize:(CGSize)size originURL:(NSURL *)originURL
//{
//    CGFloat scale = [[UIScreen mainScreen] scale];
//    NSString *sizeString;
//    if (size.width > size.height) {
//        if (IS_IPHONE_6P) {
//            sizeString = [NSString stringWithFormat:kImageFixedHeightTemplate, YH_RelatedPostImageSizeHeight * 3];
//        } else {
//            sizeString = [NSString stringWithFormat:kImageFixedHeightTemplate, YH_RelatedPostImageSizeHeight * 2];
//        }
//    } else {
//        if (IS_IPHONE_6P) {
//            sizeString = [NSString stringWithFormat:kImageFixedWidthTemplate, YH_RelatedPostImageSizeWidth * 3];
//        } else {
//            sizeString = [NSString stringWithFormat:kImageFixedWidthTemplate, YH_RelatedPostImageSizeWidth * 2];
//        }
//    }
//    
//    static NSString * const kPNGMogr2Template = @"?imageMogr2/thumbnail/%@/gravity/North/crop/%dx%d/format/png";
//    
//    NSString *kImageMogr2Template = @"";
//    NSString *imageExt = [originURL.pathExtension lowercaseString];
//    if([imageExt isEqualToString:@"gif"]){
//        kImageMogr2Template = kGifMogr2Template;
//    } else if ([imageExt isEqualToString:@"jpg"]) {
//        kImageMogr2Template = kJpgMogr2Template;
//    } else {
//        kImageMogr2Template = kPNGMogr2Template;
//    }
//    
//    NSString *sliceString = [NSString stringWithFormat:kImageMogr2Template, sizeString, (NSInteger)(YH_RelatedPostImageSizeWidth * scale), (NSInteger)(YH_RelatedPostImageSizeHeight * scale)];
//    
//    //WWAN情况下,渐进显示图片
//    if ([[AFNetworkReachabilityManager sharedManager] isReachableViaWWAN]) {
//        sliceString = [sliceString stringByAppendingString:@"/interlace/1"];
//        
//    }
//    
//    NSLog(@"related post size %@", NSStringFromCGSize(size));
//    NSLog(@"related post string is %@", [[NSURL URLWithString:sliceString relativeToURL:originURL] absoluteURL]);
//    
//    return [[NSURL URLWithString:sliceString relativeToURL:originURL] absoluteURL];
//}

+ (NSURL *)imageSliceSize:(CGSize)size originURL:(NSURL *)originURL
{
    BOOL isSlice = NO;
    NSString *sliceString = [NSString stringWithFormat:@"?imageMogr2/auto-orient"];
    CGFloat scale = [[UIScreen mainScreen] scale];
    NSString *imageExt = [originURL.pathExtension lowercaseString];
    //gif图用1.0f
    if ([imageExt isEqualToString:@"gif"]) {
        scale = 1.0f;
        
        //2014年11月7号之后的gif图,直接用原始URL
        if ([YH_ImageSliceService shouldUseOriginURL:originURL]) {
            return originURL;
        }
    }
    
    if (size.width && size.height) {
        isSlice = YES;
        sliceString = [sliceString stringByAppendingFormat:@"/thumbnail/%dx%d", (int)(size.width * scale), (int)(size.height * scale)];
    }
    else if (size.height == 0 && size.width) {
        isSlice = YES;
        sliceString = [sliceString stringByAppendingFormat:@"/thumbnail/%dx", (int)(size.width * scale)];
    }
    else if (size.width == 0 && size.height) {
        isSlice = YES;
        sliceString = [sliceString stringByAppendingFormat:@"/thumbnail/x%d", (int)(size.height * scale)];
    }
    
    //转换为jpg格式,进一步压缩图片大小
    if ([imageExt isEqualToString:@"png"] || [imageExt isEqualToString:@"bmp"]) {
        isSlice = YES;
        sliceString = [sliceString stringByAppendingString:@"/format/png"];
    } else if ([imageExt isEqualToString:@"jpg"]) {
        isSlice = YES;
#ifdef SD_WEBP
        sliceString = [sliceString stringByAppendingString:@"/format/webp"];
#else
        sliceString = [sliceString stringByAppendingString:@"/format/jpg"];
#endif
    }
    
    //WWAN情况下,渐进显示图片
    if ([[AFNetworkReachabilityManager sharedManager] isReachableViaWWAN]) {
        isSlice = YES;
        sliceString = [sliceString stringByAppendingString:@"/interlace/1"];
    }
    
    if (isSlice) {
        
        return [[NSURL URLWithString:sliceString relativeToURL:originURL] absoluteURL];
    }
    else {
        return originURL;
    }
}

//+ (NSURL *)calendarImageSliceToSize:(CGSize)toSize originImageSize:(CGSize)imageSize originURL:(NSURL *)originURL
//{
//    BOOL isSlice = NO;
//    NSString *sliceString = [NSString stringWithFormat:@"?imageMogr2"];
//    CGFloat scale = [[UIScreen mainScreen] scale];
//    NSString *imageExt = [originURL.pathExtension lowercaseString];
//    
//    if (toSize.width && toSize.height) {
//        isSlice = YES;
//        if (imageSize.width > imageSize.height) {
//            sliceString = [sliceString stringByAppendingFormat:@"/thumbnail/x%d/gravity/North/crop/%dx%d", (int)(toSize.height * scale),(int)(toSize.width * scale), (int)(toSize.height * scale)];
//        } else {
//            sliceString = [sliceString stringByAppendingFormat:@"/thumbnail/%dx/gravity/North/crop/%dx%d", (int)(toSize.width * scale),(int)(toSize.width * scale), (int)(toSize.height * scale)];
//        }
//        
//    }
//    
//    //转换为jpg格式,进一步压缩图片大小
//    if ([imageExt isEqualToString:@"png"] || [imageExt isEqualToString:@"bmp"]) {
//        isSlice = YES;
//        sliceString = [sliceString stringByAppendingString:@"/format/png"];
//    } else if ([imageExt isEqualToString:@"jpg"]) {
//        isSlice = YES;
//#ifdef SD_WEBP
//        sliceString = [sliceString stringByAppendingString:@"/format/webp"];
//#else
//        sliceString = [sliceString stringByAppendingString:@"/format/jpg"];
//#endif
//    }
//    
//    //WWAN情况下,渐进显示图片
//    if ([[AFNetworkReachabilityManager sharedManager] isReachableViaWWAN]) {
//        isSlice = YES;
//        sliceString = [sliceString stringByAppendingString:@"/interlace/1"];
//    }
//    
//    if (isSlice) {
//        return [[NSURL URLWithString:sliceString relativeToURL:originURL] absoluteURL];
//    } else {
//        return originURL;
//    }
//}

+ (CGSize)calculateImageSizeWithImageWidth:(NSInteger)imageWidth imageHeight:(NSInteger)imageHeight
{
    if (imageWidth == 0 || imageHeight == 0) {
        return CGSizeZero;
    }
    
    //图片高度(等比缩放)
    CGFloat proportion = (CGFloat)imageWidth / (CGFloat)imageHeight;
    CGFloat calWidth = imageWidth > SCREEN_WIDTH ? SCREEN_WIDTH : imageWidth;
    CGFloat calHeight = calWidth / proportion;
    
    calWidth = ceilf(calWidth);
    calHeight = ceilf(calHeight);
    return CGSizeMake(calWidth, calHeight);
}

+ (NSURL *)photoKindSliceURL:(NSURL *)originURL
{
    if (!originURL) {
        return originURL;
    }
    
    CGFloat scale = [[UIScreen mainScreen] scale];
    NSString *sliceString = [NSString stringWithFormat:@"?imageMogr2"];
    sliceString = [sliceString stringByAppendingFormat:@"?imageMogr2/thumbnail/%ldx%ld%%3E", (long)(SCREEN_WIDTH * scale),(long)(SCREEN_HEIGHT * scale)];

    //转换为jpg格式,进一步压缩图片大小
    NSString *imageExt = [originURL.pathExtension lowercaseString];
    if ([imageExt isEqualToString:@"png"] || [imageExt isEqualToString:@"bmp"]) {
        sliceString = [sliceString stringByAppendingString:@"/format/png"];
    } else if ([imageExt isEqualToString:@"jpg"]) {
#ifdef SD_WEBP
        sliceString = [sliceString stringByAppendingString:@"/format/webp"];
#else
        sliceString = [sliceString stringByAppendingString:@"/format/jpg"];
#endif
    }
    
    sliceString = [sliceString stringByAppendingString:@"/interlace/1"];
    return [[NSURL URLWithString:sliceString relativeToURL:originURL] absoluteURL];
    
}

+ (NSURL *)rulesImageSliceSize:(CGSize)size originURL:(NSURL *)originURL
{
    CGFloat scale = [[UIScreen mainScreen] scale];
    NSString *sizeString;
    if (size.width > size.height) {
        
        sizeString = [NSString stringWithFormat:@"%ldx",(long)(SCREEN_WIDTH * scale)];
        
    } else {
        
        sizeString = [NSString stringWithFormat:@"x%ld",(long)(SCREEN_HEIGHT * scale)];
        
    }
    
    NSString *sliceString = [NSString stringWithFormat:@"?imageMogr2/thumbnail/%@/gravity/North/crop/%ldx", sizeString, (long)(SCREEN_WIDTH * scale)];
    
    /*
     如果是gif图片,转换成gif格式
     否则转换为jpg格式
     */
    //WWAN情况下,渐进显示图片
    if ([[AFNetworkReachabilityManager sharedManager] isReachableViaWWAN]) {
        sliceString = [sliceString stringByAppendingString:@"/interlace/1"];
        
    }

    NSLog(@"rules image url is %@", [[NSURL URLWithString:sliceString relativeToURL:originURL] absoluteURL]);
    
    return [[NSURL URLWithString:sliceString relativeToURL:originURL] absoluteURL];
}

+ (BOOL)shouldUseOriginURL:(NSURL *)url
{
    //    NSLog(@"================================================");
    //    NSLog(@"URL: %@", url);
    //    NSLog(@"Scheme: %@", [url scheme]);
    //    NSLog(@"Host: %@", [url host]);
    //    NSLog(@"Port: %@", [url port]);
    //    NSLog(@"Path: %@", [url path]);
    //    NSLog(@"Relative path: %@", [url relativePath]);
    //    NSLog(@"Path components as array: %@", [url pathComponents]);
    //    NSLog(@"Parameter string: %@", [url parameterString]);
    //    NSLog(@"Query: %@", [url query]);
    //    NSLog(@"Fragment: %@", [url fragment]);
    //    NSLog(@"================================================");
    
    NSInteger count = [[url pathComponents] count];
    if (count < 5) {
        return NO;
    }
    
    //通过NSCALENDAR类来创建日期
    NSDateComponents *comp = [[NSDateComponents alloc]init];
    [comp setYear:2014];
    [comp setMonth:11];
    [comp setDay:07];
    
    NSCalendar *myCal = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *myDate = [myCal dateFromComponents:comp];
    
    NSInteger year = [[[url pathComponents] objectAtIndex:count - 5] integerValue];
    NSInteger month = [[[url pathComponents] objectAtIndex:count - 4] integerValue];
    NSInteger day = [[[url pathComponents] objectAtIndex:count - 3] integerValue];
    [comp setYear:year];
    [comp setMonth:month];
    [comp setDay:day];
    NSDate *urlDate = [myCal dateFromComponents:comp];
    
    if ([urlDate compare:myDate] == NSOrderedAscending) {
        return NO;
    }
    
    return YES;
}

+ (NSURL *)imageURL:(NSURL *)imageURL sliceToMaxWidth:(NSInteger)maxWidth
{
    if (!imageURL || maxWidth <= 0) {
        return imageURL;
    }
    
    NSString *sliceString = [NSString stringWithFormat:@"?imageMogr2"];
    CGFloat scale = 1;//[[UIScreen mainScreen] scale];
    NSString *imageExt = [imageURL.pathExtension lowercaseString];
    
    sliceString = [sliceString stringByAppendingFormat:@"/thumbnail/%ldx", (long)(maxWidth * scale)];
    
    //转换为jpg格式,进一步压缩图片大小
    if ([imageExt isEqualToString:@"png"] || [imageExt isEqualToString:@"bmp"]) {
        sliceString = [sliceString stringByAppendingString:@"/format/png"];
    } else if ([imageExt isEqualToString:@"jpg"]) {
#ifdef SD_WEBP
        sliceString = [sliceString stringByAppendingString:@"/format/webp"];
#else
        sliceString = [sliceString stringByAppendingString:@"/format/jpg"];
#endif
    }
    
    sliceString = [sliceString stringByAppendingString:@"/interlace/1"];
    
    NSURL *URL = [[NSURL URLWithString:sliceString relativeToURL:imageURL] absoluteURL];
    
    
    return URL;
}

+ (NSURL *)sliceToFitWidh:(CGSize)size originURL:(NSURL *)originURL
{
    NSMutableString *sliceString = [[NSMutableString alloc] init];
    CGFloat scale = [[UIScreen mainScreen] scale];
    [sliceString appendFormat:@"?imageMogr2/thumbnail/%.0fx",size.width *scale];
    [sliceString appendFormat:@"/gravity/North/crop/%.0fx%.0f",size.width *scale,size.height *scale];
    //转换为jpg格式,进一步压缩图片大小
    NSString *imageExt = [originURL.pathExtension lowercaseString];
    if ([imageExt isEqualToString:@"png"] || [imageExt isEqualToString:@"bmp"]) {
        [sliceString appendString:@"/format/png"];
    } else if ([imageExt isEqualToString:@"jpg"]) {
        
#ifdef SD_WEBP
        [sliceString appendString:@"/format/webp"];
#else
        [sliceString appendString:@"/format/jpg"];
#endif
    }
    
    [sliceString appendString:@"/interlace/1"];
    

    
    NSURL *URL = [[NSURL URLWithString:sliceString relativeToURL:originURL] absoluteURL];
    
    return URL;
}

//+ (NSURL *)sliceContentDetailImageURL:(NSURL *)originURL
//{
//    NSString *sliceString = [NSString stringWithFormat:@"?imageMogr2"];
//    NSString *imageExt = [originURL.pathExtension lowercaseString];
//    if ([imageExt isEqualToString:@"png"] || [imageExt isEqualToString:@"bmp"]) {
//        sliceString = [sliceString stringByAppendingString:@"/format/png"];
//    } else if ([imageExt isEqualToString:@"jpg"]) {
//#ifdef SD_WEBP
//        sliceString = [sliceString stringByAppendingString:@"/format/webp"];
//#else
//        sliceString = [sliceString stringByAppendingString:@"/format/jpg"];
//#endif
//    }
//    NSLog(@"content detail image urls slice string  %@", [[NSURL URLWithString:sliceString relativeToURL:originURL] absoluteURL]);
//    return [[NSURL URLWithString:sliceString relativeToURL:originURL] absoluteURL];
//}


+ (NSString *)transformImageURL:(NSString *)imageURL
{
    //判断是否存在指定参数
#ifdef SD_WEBP
    if ([[YH_Global sharedInstance] imageFormatType] == YH_ImageFormatWebp){
        if ([imageURL rangeOfString:@"/format/"].location == NSNotFound) {
            imageURL = [imageURL stringByAppendingString:@"/format/WEBP"];
        }
    }
#endif
    
    return imageURL;
}

#pragma mark - private

+ (BOOL)isHaveImageSlice:(NSString *)imageURLStr
{
    // 是否带切图指令
    if ([imageURLStr rangeOfString:@"?imageView"].location != NSNotFound
        || [imageURLStr rangeOfString:@"?imageMogr"].location != NSNotFound) {
        return YES;
    }
    return NO;
}

+ (BOOL)isJPGImage:(NSString *)imageURLStr
{
    return [imageURLStr rangeOfString:@".jpg" options:NSCaseInsensitiveSearch].location != NSNotFound;
}



@end