剪裁框和圖片剪裁

圖片處理中經(jīng)常用的圖片剪裁,就是通過剪裁框確定圖片剪裁的區(qū)域,然后剪去該區(qū)域的圖片,今天實現(xiàn)了一下,其實圖片剪裁本身不難,主要剪裁框封裝發(fā)了點時間,主要功能可以拖動四個角縮放,但不能超出父視圖,拖動四個邊單方向縮放,不能超出父視圖,拖動中間部分單單移動,不改變大小,不能超出父視圖。下面列舉一些主要代碼。
四個角的處理代碼:

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

剪裁框?qū)崿F(xiàn)的核心代碼如上,個人覺得最不好處理的是對超出父視圖的控制,要保證不能超出父視圖,個人主要用到的是通過子視圖與父視圖的交集部分的面積的改變來獲知是否超出父視圖,如果超出父視圖,就會退到之前的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這個函數(shù)的坐標(biāo)系和UIKIt的坐標(biāo)系上下顛倒,需對坐標(biāo)系處理如下:

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,023評論 25 709
  • 這篇文章主要為大家詳細(xì)介紹了iOS實現(xiàn)裁剪框和圖片剪裁功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下圖片處...
    木馬不在轉(zhuǎn)閱讀 11,609評論 9 10
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,269評論 4 61
  • 恪守之前的四步驟便可掌握分析閱讀的第一步: what a book is about and to outline...
    無夜閱讀 226評論 0 0
  • 我喜歡陽光, 因為遇見你在陽光下; 我喜歡陽光, 因為你就是我的陽光。 為你買了一個本子, 在每頁都寫上你的名字,...
    遠(yuǎn)方孤雁閱讀 271評論 0 3

友情鏈接更多精彩內(nèi)容