Skip to content

Commit

Permalink
UIImage+SuperCompress
Browse files Browse the repository at this point in the history
UIView+Genie
  • Loading branch information
Jakey committed Jan 25, 2016
1 parent 3642420 commit 07e7656
Show file tree
Hide file tree
Showing 7 changed files with 744 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Categories/Foundation/NSObject/NSObject+Reflection.m
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ -(NSArray*)methodListInfo{
NSMutableDictionary *info = [NSMutableDictionary dictionary];

Method method = methods[i];
IMP imp_f = method_getImplementation(method);
// IMP imp = method_getImplementation(method);
SEL name = method_getName(method);
// 返回方法的参数的个数
int argumentsCount = method_getNumberOfArguments(method);
Expand Down
46 changes: 46 additions & 0 deletions Categories/UIKit/UIImage/UIImage+SuperCompress.h
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
83 changes: 83 additions & 0 deletions Categories/UIKit/UIImage/UIImage+SuperCompress.m
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
31 changes: 18 additions & 13 deletions Categories/UIKit/UIScrollView/UIScrollView+EmptyDataSet.m
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ - (BOOL)dzn_isScrollAllowed
- (BOOL)dzn_isImageViewAnimateAllow
{
if (self.emptyDataSetDelegate && [self.emptyDataSetDelegate respondsToSelector:@selector(emptyDataSetShouldAnimateImageView:)]) {
return [self.emptyDataSetDelegate emptyDataSetShouldAnimateImageView:self];
return [self.emptyDataSetDelegate emptyDataSetShouldAnimateImageView:self];
}
return NO;
}
Expand Down Expand Up @@ -455,15 +455,16 @@ - (void)dzn_reloadEmptyDataSet
view.verticalSpace = [self dzn_verticalSpace];

// Configure Image
if (image) {
if ([image respondsToSelector:@selector(imageWithRenderingMode:)]) {
view.imageView.image = [image imageWithRenderingMode:renderingMode];
view.imageView.tintColor = imageTintColor;
} else {
// iOS 6 fallback: insert code to convert imaged if needed
view.imageView.image = image;
}
}
if (image) {
if ([image respondsToSelector:@selector(imageWithRenderingMode:)]) {
view.imageView.image = [image imageWithRenderingMode:renderingMode];
view.imageView.tintColor = imageTintColor;
}
else {
// iOS 6 fallback: insert code to convert imaged if needed
view.imageView.image = image;
}
}

// Configure title label
if (titleLabelString) {
Expand Down Expand Up @@ -498,12 +499,15 @@ - (void)dzn_reloadEmptyDataSet

// Configure empty dataset userInteraction permission
view.userInteractionEnabled = [self dzn_isTouchAllowed];

// Configure empty dataset fade in display
view.fadeInOnDisplay = [self dzn_shouldFadeIn];

[view setupConstraints];
[view layoutIfNeeded];

[UIView performWithoutAnimation:^{
[view layoutIfNeeded];
}];

// Configure scroll permission
self.scrollEnabled = [self dzn_isScrollAllowed];
Expand Down Expand Up @@ -883,7 +887,6 @@ - (void)setupConstraints
[self addConstraint:centerXConstraint];
[self addConstraint:centerYConstraint];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView]|" options:0 metrics:nil views:@{@"contentView": self.contentView}]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentView]|" options:0 metrics:nil views:@{@"contentView": self.contentView}]];

// When a custom offset is available, we adjust the vertical constraints' constants
if (self.verticalOffset != 0 && self.constraints.count > 0) {
Expand All @@ -892,6 +895,8 @@ - (void)setupConstraints

// If applicable, set the custom view's constraints
if (_customView) {
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentView]|" options:0 metrics:nil views:@{@"contentView": self.contentView}]];

[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[customView]|" options:0 metrics:nil views:@{@"customView":_customView}]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[customView]|" options:0 metrics:nil views:@{@"customView":_customView}]];
}
Expand Down
43 changes: 43 additions & 0 deletions Categories/UIKit/UIView/UIView+Genie.h
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
Loading

0 comments on commit 07e7656

Please sign in to comment.