美文网首页实用
剪裁框和图片剪裁

剪裁框和图片剪裁

作者: jiangamh | 来源:发表于2016-01-11 10:08 被阅读331次

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

-(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);

看看效果:

屏幕快照 2016-01-11 上午10.04.06.png

剪裁之后的图片:

屏幕快照 2016-01-11 上午10.05.44.png

简单demo:https://github.com/jiangtaidi/CropImgDemo.git

相关文章

  • 剪裁框和图片剪裁

    图片处理中经常用的图片剪裁,就是通过剪裁框确定图片剪裁的区域,然后剪去该区域的图片,今天实现了一下,其实图片剪裁本...

  • 图片剪裁选择框 LCResizableView

    最近遇到裁剪图片的需求,要求选择图片的某一区域进行剪裁,要在图片上呈现选择框来选择区域,这里选择自己开发一款 Sw...

  • 图片剪裁

    做项目是经常会遇见需要对图片进行剪裁的情况,下面来点干货 是不是很简单啊,end。。。。

  • 剪裁图片

    -(UIImage *)imageWithImage:(UIImage *)image sizeScaletoSi...

  • 图片剪裁

    图片剪裁

  • 第一堂:剪裁工具Crop Tool

    视频 剪裁工具(Crop Tool) 使用Crop Tool可以剪裁图片,旋转图片,和重新构图。 场景1)若图片中...

  • iOS图片剪裁

    项目中需要对图片进行裁剪操作,所以封装了一个简单的剪裁图片的控件,当时的设想是能够根据剪裁框的区域自动放大或缩小,...

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

    这篇文章主要为大家详细介绍了iOS实现裁剪框和图片剪裁功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下图片处...

  • 锦囊25:好用到爆的卡片风格PPT

    步骤: 1.图片处理:插入图片,左顶角对齐,按住shift键拉大;格式-剪裁,剪裁和画布一样大小;格式-艺术效果:...

  • UIImage剪裁、压缩、拉伸等处理

    1. 图片剪裁方法 直接调用如下系统现成的图片剪裁方法,封装成- (UIImage *)imageByCropTo...

网友评论

    本文标题:剪裁框和图片剪裁

    本文链接:https://www.haomeiwen.com/subject/ofbqkttx.html