iOS實現裁剪框和圖片剪裁功能_IOS

來源:互聯網
上載者:User

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

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

看看效果:

剪裁之後的圖片:

以上就是本文的全部內容,希望對大家的學習有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.