IOS Implements crop box and picture clipping function _ios

Source: Internet
Author: User

Picture clipping, which is often used in picture processing, is the area where the picture is cropped by the trim box, then cut off the area of the picture, today to achieve a bit, in fact, the picture clipping itself is not difficult, the main trim box has been issued a little time, the main function can be dragged four corner scaling, but can not exceed the parent view, drag four side single direction scaling , you can't go beyond the parent view, drag the middle section to move only, not resize, not exceed the parent view. Here are some of the main code.
four-corner processing code:

-(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 newintersectre
  ct = 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;

 }

I judge whether the parent view is exceeded by the area of the intersection of the frame in the parent view.
four-side control code:

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

 }

 }

}

Control code for moving the middle part:

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


Trim box implementation of the core code as above, personally feel the most difficult to deal with is beyond the parent view of control, to ensure that the parent view cannot be exceeded, the main use of the individual is to know whether the parent view is exceeded by changing the area of the intersection part of the child view and the parent view, and if the parent view is exceeded, the previous frame will be retired. Do not know whether there are other good ways, and some words can be exchanged.

Picture Trim Section
The code is as follows:

-(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 (@ "write error");
} groupname:@ "album name";

 }

Note here that the coordinate system of this function and the coordinate system of the Uikit are cgcontextdrawimage, and the coordinate system should be processed as follows:

CGCONTEXTTRANSLATECTM (context, 0, cropFrame.size.height);
CGCONTEXTSCALECTM (context, 1,-1);

Look at the effect:

After clipping the picture:

The above is the entire content of this article, I hope to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.