iOS实现裁剪框和图片剪裁功能

 更新时间:2016年03月22日 15:00:12   作者:jiangamh  
这篇文章主要为大家详细介绍了iOS实现裁剪框和图片剪裁功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

图片处理中经常用的图片剪裁,就是通过剪裁框确定图片剪裁的区域,然后剪去该区域的图片,今天实现了一下,其实图片剪裁本身不难,主要剪裁框封装发了点时间,主要功能可以拖动四个角缩放,但不能超出父视图,拖动四个边单方向缩放,不能超出父视图,拖动中间部分单单移动,不改变大小,不能超出父视图。下面列举一些主要代码。

四个角的处理代码:

-(void)btnPanGesture:(UIPanGestureRecognizer*)panGesture
{
 UIView *vw = panGesture.view;
 CGRect oldFrame = self.frame;
 CGRect oldIntersectRect = CGRectIntersection(self.frame, self.superview.bounds);

 CGPoint transport = [panGesture translationInView:vw];
 if (vw.tag == 4) {
 self.width = self.preFrame.size.width + transport.x;
 self.height = self.preFrame.size.height + transport.y;
 }
 else if(vw.tag == 3)
 {
 self.x = self.preFrame.origin.x + transport.x;
 self.width = self.preFrame.size.width - transport.x;
 self.height = self.preFrame.size.height + transport.y;
 }
 else if(vw.tag == 2)
 {
 self.width = self.preFrame.size.width + transport.x;
 self.y = self.preFrame.origin.y + transport.y;
 self.height = self.preFrame.size.height - transport.y;
 }
 else if(vw.tag == 1)
 {
 self.x = self.preFrame.origin.x + transport.x;
 self.width = self.preFrame.size.width - transport.x;
 self.y = self.preFrame.origin.y + transport.y;
 self.height = self.preFrame.size.height - transport.y;
 }
 if (panGesture.state == UIGestureRecognizerStateEnded) {
 self.preFrame = self.frame;
 }
 if (self.width < MinWidth || self.height < MinHeight) {
 self.frame = oldFrame;
 }
 CGRect newFrame = self.frame;
 if (newFrame.size.width * newFrame.size.height > oldFrame.size.height * oldFrame.size.width) {

 CGRect newIntersectRect = CGRectIntersection(self.frame, self.superview.bounds);
 if (newFrame.size.width * newFrame.size.height > newIntersectRect.size.width * newIntersectRect.size.height) {
 self.frame = oldFrame;
 }
 }
 self.preCenter = self.center;
}

我是通过视图于父视图的frame的交集部分的面积判断是否超出父视图的。

四个边的控制代码:

-(void)viewPanGesture:(UIPanGestureRecognizer*)panGesture
{
 UIView *vw = panGesture.view;
 CGRect oldFrame = self.frame;
 CGRect oldIntersectRect = CGRectIntersection(self.frame, self.superview.bounds);

 CGPoint transport = [panGesture translationInView:vw];
 if (vw.tag == 1) {
 self.y = self.preFrame.origin.y + transport.y;
 self.height = self.preFrame.size.height - transport.y;
 }
 else if(vw.tag == 2)
 {
 self.x = self.preFrame.origin.x + transport.x;
 self.width = self.preFrame.size.width - transport.x;
 }
 else if(vw.tag == 3)
 {
 self.height = self.preFrame.size.height + transport.y;
 }
 else if(vw.tag == 4)
 {
 self.width = self.preFrame.size.width + transport.x;
 }
 if (panGesture.state == UIGestureRecognizerStateEnded) {
 self.preFrame = self.frame;
 }
 if (self.width < MinWidth || self.height < MinHeight) {
 self.frame = oldFrame;

 }
 self.preCenter = self.center;
 CGRect newFrame = self.frame;
 if (newFrame.size.width * newFrame.size.height > oldFrame.size.height * oldFrame.size.width) {

 CGRect newIntersectRect = CGRectIntersection(self.frame, self.superview.bounds);
 if (oldIntersectRect.size.width * oldIntersectRect.size.height >= newIntersectRect.size.width * newIntersectRect.size.height) {
 self.frame = oldFrame;
 self.preCenter = self.preCenter;
 }

 }

}

中间部分移动的控制代码:

-(void)contentViewPanGestureAction:(UIPanGestureRecognizer*)panGesture
{
 CGPoint transport = [panGesture translationInView:self];
 CGRect oldFrame = self.frame;
 CGRect oldIntersectRect = CGRectIntersection(self.frame, self.superview.bounds);
 CGFloat oldMj = oldIntersectRect.size.width * oldIntersectRect.size.height;

 self.center = CGPointMake(self.preCenter.x + transport.x, self.preCenter.y + transport.y);

 if (panGesture.state == UIGestureRecognizerStateEnded) {

 self.preCenter = self.center;
 }
 CGRect newIntersectRect = CGRectIntersection(self.frame, self.superview.bounds);
 CGFloat newMj = newIntersectRect.size.width * newIntersectRect.size.height;

 if (newMj < oldMj) {
 self.frame = oldFrame;
 self.preCenter = self.center;
 }
}

剪裁框实现的核心代码如上,个人觉得最不好处理的是对超出父视图的控制,要保证不能超出父视图,个人主要用到的是通过子视图与父视图的交集部分的面积的改变来获知是否超出父视图,如果超出父视图,就会退到之前的frame,不知道是否还有其他好的方法,有的话可以一起交流一下。

图片剪裁部分

代码如下:

-(void)cropImg
{
 CGRect cropFrame = self.cropView.frame;
 CGFloat orgX = cropFrame.origin.x * (self.img.size.width / self.imgView.frame.size.width);
 CGFloat orgY = cropFrame.origin.y * (self.img.size.height / self.imgView.frame.size.height);
 CGFloat width = cropFrame.size.width * (self.img.size.width / self.imgView.frame.size.width);
 CGFloat height = cropFrame.size.height * (self.img.size.height / self.imgView.frame.size.height);
 CGRect cropRect = CGRectMake(orgX, orgY, width, height);
 CGImageRef imgRef = CGImageCreateWithImageInRect(self.img.CGImage, cropRect);

 CGFloat deviceScale = [UIScreen mainScreen].scale;
 UIGraphicsBeginImageContextWithOptions(cropFrame.size, 0, deviceScale);
 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextTranslateCTM(context, 0, cropFrame.size.height);
 CGContextScaleCTM(context, 1, -1);
 CGContextDrawImage(context, CGRectMake(0, 0, cropFrame.size.width, cropFrame.size.height), imgRef);
 UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext();
 CGImageRelease(imgRef);
 UIGraphicsEndImageContext();

 ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
 [library toolWriteImageToSavedPhotosAlbum:newImg.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
 if(error)
 {
 JGLog(@"写入出错");
 }
 } groupName:@"相册名称"];
}

这里要注意一点CGContextDrawImage这个函数的坐标系和UIKIt的坐标系上下颠倒,需对坐标系处理如下:

CGContextTranslateCTM(context, 0, cropFrame.size.height);
CGContextScaleCTM(context, 1, -1);

看看效果:

剪裁之后的图片:

以上就是本文的全部内容,希望对大家的学习有所帮助。

相关文章

  • iOS仿抖音视频加载动画效果的实现方法

    iOS仿抖音视频加载动画效果的实现方法

    这篇文章主要给大家介绍了关于iOS视频加载动画效果的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,下面随着小编来一起学习学习吧
    2018-11-11
  • 老生常谈iOS应用程序生命周期

    老生常谈iOS应用程序生命周期

    下面小编就为大家带来一篇老生常谈iOS应用程序生命周期。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • IOS 开发之UITableView 删除表格单元写法

    IOS 开发之UITableView 删除表格单元写法

    这篇文章主要介绍了IOS 开发之UITableView 删除表格单元写法的相关资料,这里提供实例帮助大家实现该功能,希望能帮助到大家,需要的朋友可以参考下
    2017-08-08
  • iOS中指纹识别常见问题汇总

    iOS中指纹识别常见问题汇总

    最近在公司做了一个app要使用指纹支付的功能,在实现过程中遇到各种坑,今天小编抽抗给大家总结把遇到问题汇总特此分享到脚本之家平台,需要的朋友参考下
    2016-12-12
  • iOS高仿微信相册界面翻转过渡动画效果

    iOS高仿微信相册界面翻转过渡动画效果

    在图片界面点击右下角的查看评论会翻转到评论界面,评论界面点击左上角的返回按钮会反方向翻转回图片界面,真正的实现方法,与传统的导航栏过渡其实只有一行代码的区别,下面小编通过本文给大家介绍下ios高仿微信相册界面翻转过渡动画效果,一起看看吧
    2016-11-11
  • iOS My97DatePicker日历使用详解

    iOS My97DatePicker日历使用详解

    这篇文章主要为大家详细介绍了iOS My97DatePicker日历的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • 一篇文章搞定iOS的Cookie存取

    一篇文章搞定iOS的Cookie存取

    Cookie中文名称叫做“小型文本文件”,指某些网站为了辨别用户身份而存储在用户本地终端上的数据(通常经过加密),下面这篇文章主要给大家介绍了关于iOS的Cookie存取的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-12-12
  • iOS利用CoreImage实现人脸识别详解

    iOS利用CoreImage实现人脸识别详解

    OS的人脸识别从iOS 5(2011)就有了,不过一直没怎么被关注过。人脸识别API允许开发者不仅可以检测人脸,也可以检测到面部的一些特殊属性,比如说微笑或眨眼。下面这篇文章主要给大家介绍了iOS利用CoreImage实现人脸识别的相关资料,需要的朋友可以参考下。
    2017-05-05
  • IOS 常见内存泄漏以及解决方案

    IOS 常见内存泄漏以及解决方案

    这篇文章主要介绍了IOS 常见内存泄漏以及解决方案的相关资料,需要的朋友可以参考下
    2017-05-05
  • Objective-C中NSArray的基本用法示例

    Objective-C中NSArray的基本用法示例

    这篇文章主要介绍了Objective-C中NSArray的基本用法示例,包括基本的排序等方法的介绍,需要的朋友可以参考下
    2015-09-09

最新评论