这里是全部代码,可以直接拷贝使用:
先看功能,如下图,裁剪前:
裁剪后:
代码:
CameraCutView.h
//
// CameraCutView.h
// ImageCut
//
// Created by huizai on 2018/7/3.
// Copyright © 2018年 huizai. All rights reserved.
//
#import
@interface CameraCutView : UIView
@property (nonatomic) UIImage * mTargetImage;
@property (nonatomic) UIImage * mResultImage;
@property (nonatomic,assign) CGFloat ImgCutHeight;
@property (nonatomic,assign) CGSize originalImageViewSize;
-(void) showCoverViewWithTargetImg;
//frame 相对于当前屏幕坐标 当前屏幕当中的裁剪范围
-(UIImage*) cutImageWithSpecificRect:(CGRect)frame;
@end
CameraCutView.m
//
// CameraCutView.m
// ImageCut
//
// Created by huizai on 2018/7/3.
// Copyright © 2018年 huizai. All rights reserved.
//
#import "CameraCutView.h"
#import "UIImage+Util.h"
@implementation CameraCutView{
UIView *_mCameraBgView;
}
- (id)init{
if (self = [super init]) {
[self initUI];
return self;
}
return nil;
}
- (void)initUI{
self.backgroundColor = [UIColor blackColor];
}
-(void) showCoverViewWithTargetImg
{
[_mCameraBgView setBackgroundColor:[UIColor clearColor]];
float scaleValue = [self getScaleNum:_mTargetImage];
_mTargetImage = [self scaleImage:_mTargetImage toScale:scaleValue];
if (_mTargetImage != nil)
{
_mCameraBgView = [[UIImageView alloc] initWithImage:_mTargetImage];
}
float _imageScale = self.frame.size.width / _mTargetImage.size.width;
_mCameraBgView.frame = CGRectMake(0, 0, _mTargetImage.size.width*_imageScale, _mTargetImage.size.height*_imageScale);
_originalImageViewSize = CGSizeMake(_mTargetImage.size.width*_imageScale, _mTargetImage.size.height*_imageScale);
_mCameraBgView.center = CGPointMake(self.frame.size.width/2.0, self.frame.size.height/2.0);
[self addSubview:_mCameraBgView];
[self addCoverView];
[self setUserGesture];
}
- (void)addCoverView{
UIImageView * coverImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, JYScreen_width, RATIO(JYScreen_height))];
UIImage * image = [UIImage imageNamed:@"zhezhao"];
image = [image resizeImageWithTop:0.9 andLeft:0 andBottom:0.95 andRight:0];
coverImage.image = image;
[self addSubview:coverImage];
}
-(void) setUserGesture
{
[_mCameraBgView setUserInteractionEnabled:YES];
//添加移动手势
UIPanGestureRecognizer *moveGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveImage:)];
[moveGes setMinimumNumberOfTouches:1];
[moveGes setMaximumNumberOfTouches:1];
[_mCameraBgView addGestureRecognizer:moveGes];
//添加缩放手势
UIPinchGestureRecognizer *scaleGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scaleImage:)];
[_mCameraBgView addGestureRecognizer:scaleGes];
// //添加旋转手势
UIRotationGestureRecognizer *rotateGes = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateImage:)];
[_mCameraBgView addGestureRecognizer:rotateGes];
}
float _lastTransX = 0.0, _lastTransY = 0.0;
- (void)moveImage:(UIPanGestureRecognizer *)sender
{
CGPoint translatedPoint = [sender translationInView:self];
if([sender state] == UIGestureRecognizerStateBegan) {
_lastTransX = 0.0;
_lastTransY = 0.0;
}
CGAffineTransform trans = CGAffineTransformMakeTranslation(translatedPoint.x - _lastTransX, translatedPoint.y - _lastTransY);
CGAffineTransform newTransform = CGAffineTransformConcat(_mCameraBgView.transform, trans);
_lastTransX = translatedPoint.x;
_lastTransY = translatedPoint.y;
if ([self isCanMove:newTransform])
{
_mCameraBgView.transform = newTransform;
}
}
float _lastScale = 1.0;
- (void)scaleImage:(UIPinchGestureRecognizer *)sender
{
if([sender state] == UIGestureRecognizerStateBegan) {
_lastScale = 1.0;
return;
}
CGFloat scale = [sender scale]/_lastScale;
CGAffineTransform currentTransform = _mCameraBgView.transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[_mCameraBgView setTransform:newTransform];
_lastScale = [sender scale];
}
float _lastRotation = 0.0;
- (void)rotateImage:(UIRotationGestureRecognizer *)sender
{
if([sender state] == UIGestureRecognizerStateEnded) {
_lastRotation = 0.0;
return;
}
CGFloat rotation = -_lastRotation + [sender rotation];
CGAffineTransform currentTransform = _mCameraBgView.transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[_mCameraBgView setTransform:newTransform];
_lastRotation = [sender rotation];
}
/***
方法名称:cutImageWithSpecificRect
方法用途:根据特定的区域对图片进行裁剪
方法说明:核心裁剪方法CGImageCreateWithImageInRect(CGImageRef image,CGRect rect)
***/
-(UIImage*) cutImageWithSpecificRect:(CGRect)frame;
{
float zoomScale = [[_mCameraBgView.layer valueForKeyPath:@"transform.scale.x"] floatValue];
float rotate = [[_mCameraBgView.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
float _imageScale = _mTargetImage.size.width/_originalImageViewSize.width;
//裁剪区域的Size
// CGSize cropSize = CGSizeMake(320.0/zoomScale, 120.0/zoomScale);
CGSize cropSize = CGSizeMake(frame.size.width/zoomScale, frame.size.height/zoomScale);
//裁剪区域的Origin
// CGPoint cropperViewOrigin = CGPointMake((0.0 - _mCameraBgView.frame.origin.x)/zoomScale,
// ((JYScreen_height - _ImgCutHeight)/2 - _mCameraBgView.frame.origin.y)/zoomScale);
CGPoint cropperViewOrigin = CGPointMake((0.0 - _mCameraBgView.frame.origin.x + frame.origin.x)/zoomScale,
(-_mCameraBgView.frame.origin.y)/zoomScale + frame.origin.y/zoomScale);
if((NSInteger)cropSize.width % 2 == 1)
{
cropSize.width = ceil(cropSize.width);
}
if((NSInteger)cropSize.height % 2 == 1)
{
cropSize.height = ceil(cropSize.height);
}
CGRect CropRectinImage = CGRectMake((NSInteger)(cropperViewOrigin.x*_imageScale) ,(NSInteger)( cropperViewOrigin.y*_imageScale), (NSInteger)(cropSize.width*_imageScale),(NSInteger)(cropSize.height*_imageScale));
UIImage *rotInputImage = [_mTargetImage imageRotatedByRadians:rotate];
CGImageRef tmp = CGImageCreateWithImageInRect([rotInputImage CGImage], CropRectinImage);
self.mResultImage = [UIImage imageWithCGImage:tmp scale:_mTargetImage.scale orientation:_mTargetImage.imageOrientation];
CGImageRelease(tmp);
return self.mResultImage;
}
-(BOOL) isCanMove:(CGAffineTransform ) newTransform
{
if (_mCameraBgView.frame.size.height/2 - fabs(newTransform.ty)<=0 ||
_mCameraBgView.frame.size.width/2 - fabs(newTransform.tx) <=0)
{
return NO;
} else
{
return YES;
}
}
/***
方法名称:scaleImage:toScale:
方法用途:图片的伸缩处理
方法说明:scaleSize:放大或缩小的倍数
***/
- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
{
UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
[image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
/***
方法名称:getScaleNum:
方法用途:根据图片的宽度为基准,来获取图片伸缩放大的倍数
方法说明:
***/
-(float) getScaleNum:(UIImage *) targetImg
{
CGRect r = [UIScreen mainScreen].bounds;
float preWidth = targetImg.size.width;
float scaleValue = 1;
scaleValue = r.size.width/preWidth;
return scaleValue;
}
//图片复原
- (void)reset
{
_mCameraBgView.transform = CGAffineTransformIdentity;
}
@end
UIImage+Util.h
//
// UIImage+Util.h
// ImageCut
//
// Created by huizai on 2018/7/3.
// Copyright © 2018年 huizai. All rights reserved.
//
#import
@interface UIImage (Util)
//设置的外围不变形内部平铺拉伸
- (UIImage*)resizeImageWithTop:(CGFloat)top andLeft:(CGFloat)left andBottom:(CGFloat)bottom andRight:(CGFloat)right;
- (UIImage *)imageRotatedByRadians:(CGFloat)radians;
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees;
@end
UIImage+Util.m
//
// UIImage+Util.m
// ImageCut
//
// Created by huizai on 2018/7/3.
// Copyright © 2018年 huizai. All rights reserved.
//
#import "UIImage+Util.h"
CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
CGFloat RadiansToDegrees(CGFloat radians) {return radians * 180/M_PI;};
@implementation UIImage (Util)
//设置的外围不变形内部平铺拉伸
- (UIImage*)resizeImageWithTop:(CGFloat)top andLeft:(CGFloat)left andBottom:(CGFloat)bottom andRight:(CGFloat)right{
UIImage *image = self;
// 设置端盖的值
CGFloat _top = image.size.height * top;
CGFloat _left = image.size.width * left;
CGFloat _bottom = image.size.height * bottom;
CGFloat _right = image.size.width * right;
// 设置端盖的值
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(_top, _left, _bottom, _right);
// 设置拉伸的模式
UIImageResizingMode mode = UIImageResizingModeStretch;
// 拉伸图片
UIImage *newImage = [image resizableImageWithCapInsets:edgeInsets resizingMode:mode];
return newImage;
}
- (UIImage *)imageRotatedByRadians:(CGFloat)radians
{
return [self imageRotatedByDegrees:RadiansToDegrees(radians)];
}
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees
{
// calculate the size of the rotated view's containing box for our drawing space
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
// Move the origin to the middle of the image so we will rotate and scale around the center.
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
// // Rotate the image context
CGContextRotateCTM(bitmap, DegreesToRadians(degrees));
// Now, draw the rotated/scaled image into the context
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
@end