forked from shaojiankui/JKCategories
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UIView+Genie
- Loading branch information
Jakey
committed
Jan 25, 2016
1 parent
3642420
commit 07e7656
Showing
7 changed files
with
744 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// UIImage+SuperCompress.h | ||
// iOS-Categories (https://github.com/shaojiankui/iOS-Categories) | ||
// | ||
// Created by Jakey on 16/1/22. | ||
// Copyright © 2016年 Jakey. All rights reserved. | ||
// https://github.com/CompletionHandler/CYImageCompress | ||
|
||
//usage [UIImage compressImage:image toMaxLength:512*1024*8 maxWidth:1024]; | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
@interface UIImage (SuperCompress) | ||
/** | ||
* 压缩上传图片到指定字节 | ||
* | ||
* @param image 压缩的图片 | ||
* @param maxLength 压缩后最大字节大小 | ||
* | ||
* @return 压缩后图片的二进制 | ||
*/ | ||
+ (NSData *)compressImage:(UIImage *)image toMaxLength:(NSInteger) maxLength maxWidth:(NSInteger)maxWidth; | ||
|
||
/** | ||
* 获得指定size的图片 | ||
* | ||
* @param image 原始图片 | ||
* @param newSize 指定的size | ||
* | ||
* @return 调整后的图片 | ||
*/ | ||
+ (UIImage *)resizeImage:(UIImage *) image withNewSize:(CGSize) newSize; | ||
|
||
/** | ||
* 通过指定图片最长边,获得等比例的图片size | ||
* | ||
* @param image 原始图片 | ||
* @param imageLength 图片允许的最长宽度(高度) | ||
* | ||
* @return 获得等比例的size | ||
*/ | ||
+ (CGSize)scaleImage:(UIImage *) image withLength:(CGFloat) imageLength; | ||
|
||
///对指定图片进行拉伸 | ||
+ (UIImage*)resizableImage:(NSString *)name; | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// UIImage+SuperCompress.m | ||
// iOS-Categories (https://github.com/shaojiankui/iOS-Categories) | ||
// | ||
// Created by Jakey on 16/1/22. | ||
// Copyright © 2016年 Jakey. All rights reserved. | ||
// | ||
|
||
#import "UIImage+SuperCompress.h" | ||
|
||
@implementation UIImage (SuperCompress) | ||
+ (UIImage*)resizableImage:(NSString *)name | ||
{ | ||
UIImage *normal = [UIImage imageNamed:name]; | ||
|
||
CGFloat imageW = normal.size.width * 0.5; | ||
CGFloat imageH = normal.size.height * 0.5; | ||
return [normal resizableImageWithCapInsets:UIEdgeInsetsMake(imageH, imageW, imageH, imageW)]; | ||
} | ||
|
||
|
||
+ (NSData *)compressImage:(UIImage *)image toMaxLength:(NSInteger)maxLength maxWidth:(NSInteger)maxWidth{ | ||
NSAssert(maxLength > 0, @"图片的大小必须大于 0"); | ||
NSAssert(maxWidth > 0, @"图片的最大边长必须大于 0"); | ||
|
||
CGSize newSize = [self scaleImage:image withLength:maxWidth]; | ||
UIImage *newImage = [self resizeImage:image withNewSize:newSize]; | ||
|
||
CGFloat compress = 0.9f; | ||
NSData *data = UIImageJPEGRepresentation(newImage, compress); | ||
|
||
while (data.length > maxLength && compress > 0.01) { | ||
compress -= 0.02f; | ||
|
||
data = UIImageJPEGRepresentation(newImage, compress); | ||
} | ||
return data; | ||
} | ||
|
||
+ (UIImage *) resizeImage:(UIImage *) image withNewSize:(CGSize) newSize{ | ||
|
||
UIGraphicsBeginImageContext(newSize); | ||
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; | ||
|
||
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); | ||
UIGraphicsEndImageContext(); | ||
|
||
return newImage; | ||
} | ||
|
||
+ (CGSize) scaleImage:(UIImage *) image withLength:(CGFloat) imageLength{ | ||
|
||
CGFloat newWidth = 0.0f; | ||
CGFloat newHeight = 0.0f; | ||
CGFloat width = image.size.width; | ||
CGFloat height = image.size.height; | ||
|
||
if (width > imageLength || height > imageLength){ | ||
|
||
if (width > height) { | ||
|
||
newWidth = imageLength; | ||
newHeight = newWidth * height / width; | ||
|
||
}else if(height > width){ | ||
|
||
newHeight = imageLength; | ||
newWidth = newHeight * width / height; | ||
|
||
}else{ | ||
|
||
newWidth = imageLength; | ||
newHeight = imageLength; | ||
} | ||
|
||
}else{ | ||
return CGSizeMake(width, height); | ||
} | ||
|
||
return CGSizeMake(newWidth, newHeight); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// UIView+Genie.h | ||
// BCGenieEffect | ||
// | ||
// Created by Bartosz Ciechanowski on 23.12.2012. | ||
// Copyright (c) 2012 Bartosz Ciechanowski. All rights reserved. | ||
// | ||
// An OSX style genie effect inside your iOS app. | ||
//https://github.com/Ciechan/BCGenieEffect | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
typedef NS_ENUM(NSUInteger, BCRectEdge) { | ||
BCRectEdgeTop = 0, | ||
BCRectEdgeLeft = 1, | ||
BCRectEdgeBottom = 2, | ||
BCRectEdgeRight = 3 | ||
}; | ||
|
||
@interface UIView (Genie) | ||
|
||
/* | ||
* After the animation has completed the view's transform will be changed to match the destination's rect, i.e. | ||
* view's transform (and thus the frame) will change, however the bounds and center will *not* change. | ||
*/ | ||
|
||
- (void)genieInTransitionWithDuration:(NSTimeInterval)duration | ||
destinationRect:(CGRect)destRect | ||
destinationEdge:(BCRectEdge)destEdge | ||
completion:(void (^)())completion; | ||
|
||
|
||
|
||
/* | ||
* After the animation has completed the view's transform will be changed to CGAffineTransformIdentity. | ||
*/ | ||
|
||
- (void)genieOutTransitionWithDuration:(NSTimeInterval)duration | ||
startRect:(CGRect)startRect | ||
startEdge:(BCRectEdge)startEdge | ||
completion:(void (^)())completion; | ||
|
||
@end |
Oops, something went wrong.