转:http://www.cnblogs.com/xiaobaizhu/archive/2013/07/03/3170101.html

该控件有旋转,缩放,拖动,剪裁的功能,封装成了一个ImageCropperView类

需要导入的库:QuartzCore.framework

ImageCopperView.h

#import <UIKit/UIKit.h>

@protocol ImageCropperDelegate;

@interface ImageCropperView : UIView {
UIImageView *imageView; id <ImageCropperDelegate> delegate;
} @property (nonatomic, retain) UIImage *image;
@property (nonatomic, retain) UIImage *croppedImage; @property (nonatomic, assign) id <ImageCropperDelegate> delegate; @property (nonatomic, assign) BOOL enable;
@property (nonatomic, assign) BOOL isPaning; - (void)setup;
- (void)finishCropping;
- (void)reset; @end @protocol ImageCropperDelegate <NSObject>
- (void)changeMoveStateWithCropper:(UIPanGestureRecognizer*)gesture Crop:(ImageCropperView*)imageCrop;
@end

ImageCopperView.m

#import "ImageCropperView.h"
#import <QuartzCore/QuartzCore.h>
#include <math.h>
#import "UIImage+Rotation.h" @interface ImageCropperView()
{
@private
CGSize _originalImageViewSize;
} @property (nonatomic, retain) UIImageView *imageView;
@end @implementation ImageCropperView @synthesize imageView, image = _image, delegate, croppedImage; - (void)setup
{
_enable = YES;
self.clipsToBounds = YES;
self.backgroundColor = [UIColor clearColor]; self.imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.height)] autorelease];
imageView.userInteractionEnabled = YES;
[self addSubview:imageView]; UIRotationGestureRecognizer *rotateGes = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateImage:)];
[imageView addGestureRecognizer:rotateGes];
[rotateGes release]; UIPinchGestureRecognizer *scaleGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scaleImage:)];
[imageView addGestureRecognizer:scaleGes];
[scaleGes release]; UIPanGestureRecognizer *moveGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveImage:)];
[moveGes setMinimumNumberOfTouches:1];
[moveGes setMaximumNumberOfTouches:1];
[imageView addGestureRecognizer:moveGes];
[moveGes release];
} - (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame]; if (self) {
self.frame = frame;
[self setup];
} return self;
} float _lastTransX = 0.0, _lastTransY = 0.0;
- (void)moveImage:(UIPanGestureRecognizer *)sender
{
_isPaning = YES;
if (delegate&&[delegate respondsToSelector:@selector(changeMoveStateWithCropper:Crop:)]) {
[delegate changeMoveStateWithCropper:sender Crop:self];
}else{
return;
}
if (sender.numberOfTouches != 1||_enable == NO) {
return;
}
//获取在视图中手势的触点位置
CGPoint translatedPoint = [sender translationInView:self]; if([sender state] == UIGestureRecognizerStateBegan) {
_lastTransX = 0.0;
_lastTransY = 0.0;
} CGAffineTransform trans = CGAffineTransformMakeTranslation(translatedPoint.x - _lastTransX, translatedPoint.y - _lastTransY);
//CGAffineTransformConcat将imageView.transform和trans两个动画连续起来
CGAffineTransform newTransform = CGAffineTransformConcat(imageView.transform, trans);
_lastTransX = translatedPoint.x;
_lastTransY = translatedPoint.y;
NSLog(@"_lastTransX==%f,_lastTransY==%f",_lastTransX,_lastTransY);
imageView.transform = newTransform;
} float _lastScale = 1.0;
- (void)scaleImage:(UIPinchGestureRecognizer *)sender
{
_isPaning = NO;
if (sender.numberOfTouches != 2||_enable == NO) {
return;
} if([sender state] == UIGestureRecognizerStateBegan) { _lastScale = 1.0;
return;
} CGFloat scale = [sender scale]/_lastScale; CGAffineTransform currentTransform = imageView.transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[imageView setTransform:newTransform]; _lastScale = [sender scale];
} float _lastRotation = 0.0;
- (void)rotateImage:(UIRotationGestureRecognizer *)sender
{
_isPaning = NO;
if (sender.numberOfTouches != 2||_enable == NO) {
return;
} if([sender state] == UIGestureRecognizerStateEnded) { _lastRotation = 0.0;
return;
} CGFloat rotation = -_lastRotation + [sender rotation]; CGAffineTransform currentTransform = imageView.transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[imageView setTransform:newTransform]; _lastRotation = [sender rotation]; } - (void)setImage:(UIImage *)image
{
if (_image != image) {
_image = [image retain];
} float _imageScale = self.frame.size.width / image.size.width;
self.imageView.frame = CGRectMake(0, 0, image.size.width*_imageScale, image.size.height*_imageScale);
_originalImageViewSize = CGSizeMake(image.size.width*_imageScale, image.size.height*_imageScale);
imageView.image = image;
imageView.center = CGPointMake(self.frame.size.width/2.0, self.frame.size.height/2.0);
} - (void)finishCropping {
float zoomScale = [[self.imageView.layer valueForKeyPath:@"transform.scale.x"] floatValue];
float rotate = [[self.imageView.layer valueForKeyPath:@"transform.rotation.z"] floatValue]; float _imageScale = _image.size.width/_originalImageViewSize.width;
CGSize cropSize = CGSizeMake(self.frame.size.width/zoomScale, self.frame.size.height/zoomScale);
CGPoint cropperViewOrigin = CGPointMake((0.0 - self.imageView.frame.origin.x)/zoomScale,
(0.0 - self.imageView.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 = [self.image imageRotatedByRadians:rotate];
CGImageRef tmp = CGImageCreateWithImageInRect([rotInputImage CGImage], CropRectinImage);
self.croppedImage = [UIImage imageWithCGImage:tmp scale:self.image.scale orientation:self.image.imageOrientation];
CGImageRelease(tmp);
} - (void)reset
{
self.imageView.transform = CGAffineTransformIdentity;
} - (void)dealloc {
self.image = nil;
self.croppedImage = nil;
self.imageView = nil; [super dealloc];
} @end

对UIImage添加了一个category

UIImage+Rotation.h

#import <UIKit/UIKit.h>

@interface UIImage (Rotation)

- (UIImage *)imageRotatedByRadians:(CGFloat)radians;
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees; @end

UIImage+Rotation.m

#import "UIImage+Rotation.h"

/************
角度=弧度/Pi*180
弧度=角度/180*Pi
*************/ CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
CGFloat RadiansToDegrees(CGFloat radians) {return radians * 180/M_PI;}; @implementation UIImage (Rotation) - (UIImage *)imageRotatedByRadians:(CGFloat)radians
{
return [self imageRotatedByDegrees:RadiansToDegrees(radians)];
} - (UIImage *)imageRotatedByDegrees:(CGFloat)degrees
{
/*****
CGAffineTransformMakeRotation
通过指定角度来创建一个旋转矩阵
CGAffineTransformRotate
在已存在的矩阵中使用旋转
*****/
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
//给view旋转角度
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
[rotatedViewBox release];
//开始编辑图形上下文
UIGraphicsBeginImageContext(rotatedSize);
//定义一个图形上下文
CGContextRef bitmap = UIGraphicsGetCurrentContext();
//沿x轴移动rotatedSize.width/2,y轴移动rotatedSize.height
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
//以原点(左下角)为中心旋转DegreesToRadians(degrees)弧度,正角度逆时针,负角度顺时针
CGContextRotateCTM(bitmap, DegreesToRadians(degrees));
//缩放x轴,y轴方向
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
UIImage *resImage = UIGraphicsGetImageFromCurrentImageContext();
//结束绘制
UIGraphicsEndImageContext();
return resImage;
} @end;

ViewController.m

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "ImagecropperView.h" @interface ViewController ()<ImageCropperDelegate>{
} @property (nonatomic, retain) IBOutlet ImageCropperView *cropper;
@property (nonatomic, retain) IBOutlet UIImageView *result;
@property (retain, nonatomic) IBOutlet UIImageView *resultSecond;
@property (nonatomic, retain) IBOutlet UIButton *btn;
@property (retain, nonatomic) IBOutlet ImageCropperView *cropperSecond; @property (retain, nonatomic) IBOutlet UIButton *cropButton; @end @implementation ViewController
//@synthesize cropper, result, btn; - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_cropper.layer.borderWidth = 1.0;
_cropper.layer.borderColor = [UIColor blueColor].CGColor;
_cropper.delegate = self;
[_cropper setup];
_cropper.image = [UIImage imageNamed:@"2.jpg"];
[_btn addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside]; _cropperSecond.layer.borderColor = [UIColor blackColor].CGColor;
_cropperSecond.layer.borderWidth = 2.0;
_cropperSecond.delegate = self;
[_cropperSecond setup];
_cropperSecond.image = [UIImage imageNamed:@"1.jpg"];
[_cropButton addTarget:self action:@selector(tapCropButton) forControlEvents:UIControlEventTouchUpInside];
} - (void)buttonClicked
{
if ([_btn.currentTitle isEqualToString:@"Crop1"]) {
[_cropper finishCropping];//保存
_result.image = _cropper.croppedImage;
_cropper.hidden = YES;
[_btn setTitle:@"Back" forState:UIControlStateNormal];
[_btn setTitle:@"Back" forState:UIControlStateHighlighted];
}else
{
[_cropper reset];
_cropper.hidden = NO;
[_btn setTitle:@"Crop1" forState:UIControlStateNormal];
[_btn setTitle:@"Crop1" forState:UIControlStateHighlighted];
_result.image = nil;
}
_cropperSecond.enable = YES;
_cropper.enable = YES;
} - (void)tapCropButton{
if ([_cropButton.currentTitle isEqualToString:@"Crop2"]) {
[_cropperSecond finishCropping];
_cropperSecond.enable = NO;
_resultSecond.image = _cropperSecond.croppedImage;
_cropperSecond.hidden = YES;
[_cropButton setTitle:@"Back" forState:UIControlStateNormal];
[_cropButton setTitle:@"Back" forState:UIControlStateHighlighted];
}else
{
[_cropperSecond reset]; _cropperSecond.hidden = NO;
[_cropButton setTitle:@"Crop2" forState:UIControlStateNormal];
[_cropButton setTitle:@"Crop2" forState:UIControlStateHighlighted];
_resultSecond.image = nil;
}
_cropperSecond.enable = YES;
_cropper.enable = YES;
} #pragma mark - ImageCropperDelegate
- (void)changeMoveStateWithCropper:(UIPanGestureRecognizer*)gesture Crop:(ImageCropperView*)imageCrop{
if (gesture.state == UIGestureRecognizerStateEnded) {
NSLog(@"点击编辑器结束,两个_cropper都可以进行编辑");
_cropperSecond.enable = YES;
_cropper.enable = YES;
}
} - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event;
{
//判断点击在控件上
UITouch *touch = [touches anyObject];
if ([_cropper pointInside:[touch locationInView:_cropper] withEvent:nil]) {
NSLog(@"_cropper1 被触摸,禁用_cropper2");
_cropperSecond.enable = NO;
}else if ([_cropperSecond pointInside:[touch locationInView:_cropperSecond] withEvent:nil]){
NSLog(@"_cropper2 被触摸,禁用_cropper1");
_cropper.enable = NO;
}
} - (void)dealloc {
[_cropperSecond release];
[_cropButton release];
[_resultSecond release];
[super dealloc];
}
- (void)viewDidUnload {
[self setCropperSecond:nil];
[self setCropButton:nil];
[self setResultSecond:nil];
[super viewDidUnload];
}
@end

下面是截图

最后要注意,因为我是用xib做的,拖上去的UIView要将其Class改成ImageCropperView

ios 照片编辑的view封装的更多相关文章

  1. iOS视频编辑SDK

    IOS视频编辑SDK接入说明 一.名词解释 分辨率:用于计算机视频处理的图像,以水平和垂直方向上所能显示的像素数来表示分辨率.常见视频分辨率的有1080P即1920x1080,720P即1080x72 ...

  2. IOS 开发中 Whose view is not in the window hierarchy 错误的解决办法

    在 IOS 开发当中经常碰到 whose view is not in the window hierarchy 的错误,该错误简单的说,是由于 "ViewController" ...

  3. IOS照片颠倒分析及移动/页面端的处理策略和思路

    前言: 前几天, 写了一篇关于IOS手机上传照片颠倒的技术分析文章: IOS照片颠倒分析及PHP服务端的处理. 不过其思路是从服务器来进行处理的, 这种做法相当普遍. 今天来讲述下, 如何从移动端/页 ...

  4. 关于报错:'sharedApplication' is unavailable: not available on iOS (App Extension) - Use view controller based

    最近在看Extension相关知识的时候,自己写了个小demo 发现[UIApplication sharedApplication]这个方法敲不出来了, 总是报错:'sharedApplicatio ...

  5. IOS—通过ChildViewController实现view的切换

    IOS-通过ChildViewController实现view的切换 在以前,一个UIViewController的View可能有很多小的子view.这些子view很多时候被盖在最后,我们在最外层Vi ...

  6. 详解iOS开发之自定义View

    iOS开发之自定义View是本文要将介绍的内容,iOS SDK中的View是UIView,我们可以很方便的自定义一个View.创建一个 Window-based Application程序,在其中添加 ...

  7. iOS 容器控制器 (Container View Controller)

    iOS 容器控制器 (Container View Controller) 一个控制器包含其他一个或多个控制器,前者为容器控制器 (Container View Controller),后者为子控制器 ...

  8. Luminar 3 for Mac(照片编辑工具)v3.1.0中文特别版

    Luminar for Mac是一款多功能照片编辑软件,使用独特的AI工具加快速度,具备AI Sky Enhancer.Accent AI.太阳光线等创新功能.当然也保留了原有的功能,帮助你轻松的修复 ...

  9. iOS9中找不到XXX.dylib 与 is unavailable no availabel on ios (app extension) - use view controller 的解决办法

    在 iOS9 中现在找不到 XXX.dylib 了,比如libz.tbd  如果要用到 libz.dylib,可以用下面的办法,来自 Stack Overflow. Go to Build Phase ...

随机推荐

  1. Unity3D Development模式下的一个小问题

    今天客户提交了一个反馈,说测试版本的应用在按下电源键的时候屏幕变黑,然后重新按下电源键启动的时候发现没有出现屏幕锁屏的情况,直接回到应用界面. 我这边看了一下,发现如果装了360之类的手机助手就没这个 ...

  2. sp_MSforeachtable使用方法 查看库中所有表的空间大小

    sp_MSforeachtable使用方法 1)说明系统存储过程sp_MSforeachtable和sp_MSforeachdb,是微软提供的两个不公开的存储过程,从ms sql 6.5开始.存放在S ...

  3. jsp中文件下载的实现

    jsp中实现文件下载的最简单的方式是在网页上做超级链接,如:<a href="music/abc.mp3">点击下载</a>.但是这样服务器上的目录资源会直 ...

  4. Hibernate3中将指定的HQL语句转换成SQL语句

    import org.hibernate.engine.SessionFactoryImplementor; import org.hibernate.hql.ast.QueryTranslatorI ...

  5. POJ 2407 Relatives(欧拉函数)

    题目链接 题意 : 求小于等于n中与n互质的数的个数. 思路 : 看数学的时候有一部分是将欧拉函数的,虽然我没怎么看懂,但是模板我记得了,所以直接套了一下模板. 这里是欧拉函数的简介. #includ ...

  6. java中的freopen

    在做ACM题目的时候,为节省输入测试数据的时间,我们通常将数据复制到一个文本文档里,然后从文档里读出,避免在控制台一个数据一个数据的输入. 之前一直用的C/C++,freopen用起来很方便,如下: ...

  7. flexbox弹性盒子布局

    混合划分 demo1,css: #demo1{ width: 100%; background: #ccc; display: -webkit-flex;/*表示使用弹性布局*/ } #demo1 . ...

  8. c/c++优秀博文

    C进阶指南(1):整型溢出和类型提升.内存申请和管理 http://blog.jobbole.com/72830/ 软件开发中应避免的10个问题

  9. ios开发跳转

    如果我的是A->B->C后,我想直接从 C->A 应该怎么做???这是我的问题    看了这个帖子不太明白      这是10楼的解决办法 , 虽然 写的很清楚 ,但是还是没懂啊  ...

  10. 单交换机VLAN虚拟局域网划分

    1.下载Cisco模拟器 Packet Tracer 是由Cisco公司发布的一个辅助学习工具,为学习CCNA课程的网络初学者去设计.配置.排除网络故障提供了网络模拟环境.学生可在软件的图形用户界面上 ...