不多说,上代码
.h文件
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, JWCropImageStyle){
JWCropImageStyleRight =0, // 右半部分
JWCropImageStyleCenter =1, // 中间部分
JWCropImageStyleLeft =2, // 左半部分
JWCropImageStyleRightOneOfThird =3, // 右侧三分之一部分
JWCropImageStyleCenterOneOfThird =4, // 中间三分之一部分
JWCropImageStyleLeftOneOfThird =5, // 左侧三分之一部分
JWCropImageStyleRightQuarter =6, // 右侧四分之一部分
JWCropImageStyleCenterRightQuarter =7, // 中间右侧四分之一部分
JWCropImageStyleCenterLeftQuarter =8, // 中间左侧四分之一部分
JWCropImageStyleLeftQuarter =9, // 左侧四分之一部分
JWIDCardImageStyleCenter =10, // id card
};
@interface UIImage (Extension)
//@interface UIImage (Crop)
- (UIImage *)imageByCroppingWithStyle:(JWCropImageStyle)style;
@end
.m文件
#import "UIImage+crop.h"
@implementation UIImage (Extension)
- (UIImage *)imageByCroppingWithStyle:(JWCropImageStyle)style
{
CGRect rect;
switch (style) {
case JWCropImageStyleLeft:
rect = CGRectMake(0, 0, self.size.width/2, self.size.height);
break;
case JWCropImageStyleCenter:
rect = CGRectMake(0, self.size.height/4, self.size.width, self.size.height/2);
break;
case JWCropImageStyleRight:
rect = CGRectMake(self.size.width/2, 0, self.size.width/2, self.size.height);
break;
case JWCropImageStyleLeftOneOfThird:
rect = CGRectMake(0, 0, self.size.width/3, self.size.height);
break;
case JWCropImageStyleCenterOneOfThird:
rect = CGRectMake(self.size.width/3, 0, self.size.width/3, self.size.height);
break;
case JWCropImageStyleRightOneOfThird:
rect = CGRectMake(self.size.width/3*2, 0, self.size.width/3, self.size.height);
break;
case JWCropImageStyleLeftQuarter:
rect = CGRectMake(0, 0, self.size.width/4, self.size.height);
break;
case JWCropImageStyleCenterLeftQuarter:
rect = CGRectMake(self.size.width/4, 0, self.size.width/4, self.size.height);
break;
case JWCropImageStyleCenterRightQuarter:
rect = CGRectMake(self.size.width/4*2, 0, self.size.width/4, self.size.height);
break;
case JWCropImageStyleRightQuarter:
rect = CGRectMake(self.size.width/4*3, 0, self.size.width/4, self.size.height);
break;
case JWIDCardImageStyleCenter:
// rect = CGRectMake(0, self.size.height/4, self.size.width, self.size.height/2);
rect = CGRectMake(self.size.width/4+100, 200, self.size.width/2, self.size.height);
break;
default:
break;
}
CGImageRef imageRef = self.CGImage;
CGImageRef imagePartRef = CGImageCreateWithImageInRect(imageRef, rect);
UIImage *cropImage = [UIImage imageWithCGImage:imagePartRef];
CGImageRelease(imagePartRef);
return cropImage;
}
@end
这个是之前在网上找的,忘了是谁写的,如果谁知道,麻烦告诉我一下原作者的链接