Achieve results
- Operation Steps
- Draws a rectangle, pops up a alertview, prompts whether to save the picture
- Click "Yes" to save the picture to the album
- View a saved picture in an album
Implementation ideas
- Add a ImageView to the view on the controller, set the picture
- Add a pan gesture to the view on the controller
- Track pan gestures, draw a rectangular box (
图片的剪切区域
)
- At the end of the pan gesture, Alertview prompts "Do you want to save the picture to the album?" ”
- Click "Yes" to save the image
- Click "No" to temporarily do nothing
Implementation steps
Add a ImageView () to the controller's view via storyboard 设置图片
and have the attribute in the controller's. m file
@property (weaknonatomicIBOutletUIImageView *imageView;
Set the clipping area of a picture drawn by gesture
- ClipView the clipping area of a picture as a member property
@property (nonatomicweakUIView *clipView;
- Create a ClipView by lazy loading, and initialize
-(uiview *) clipView{ //if ClipView is created, create a if (_clipview = = nil ) {uiview *view = [[uiview alloc] init]; _clipview = view; //set ClipView background color and transparency view.backgroundcolor = [uicolor Blackcolor]; View.alpha = 0.5 ; //adds ClipView to the controller's view, at which point the ClipView is not displayed (no frame is set) [self .view Addsubview:_clipview]; } return _clipview;}
Add pan gestures to the view of the controller, track pan gestures, draw a picture clipping area
/**创建手势**/UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; /** *每当pan手势的位置发生变化,就会调用pan:方法,并将手势作为参数传递 *//**添加手势**/[self.view addGestureRecognizer:pan];
- Add member properties to record the point at which the pan gesture started
@property (nonatomicassignCGPoint startPoint;
- The movement of the listening gesture
- (void) Pan: (Uipangesturerecognizer *) pan{cgpoint endPoint = Cgpointzero;if(pan.state = = Uigesturerecognizerstatebegan) {/** start Clicking, record the starting point of the gesture **/Self.startpoint = [Pan LocationInView:self.view]; }Else if(pan.state = = uigesturerecognizerstatechanged) {/** dynamically changes the value of the end point when the gesture moves, and calculates the rectangular area between the start and end points **/EndPoint = [Pan LocationInView:self.view];//Calculate width and height of rectangular areaCGFloat w = endpoint.x-self.startpoint.x; CGFloat h = endpoint.y-self.startpoint.y;//Calculate frame for rectangular areaCGRect clipRect = CGRectMake (Self.startpoint.x, Self.startpoint.y, W, h);//Set frame for clipping areaSelf.clipView.frame = ClipRect; }Else if(pan.state = = uigesturerecognizerstateended) {/** If the gesture stops, the picture contents of the clipping area are drawn into the graphical context **/ //Open Bitmap contextUigraphicsbeginimagecontextwithoptions (Self.imageView.bounds.size, NO,0);//Create a closed path with size equal to the size of the clipping areaUibezierpath *path = [Uibezierpath bezierPathWithRect:self.clipView.frame];//settings out of the content is not displayed,[Path Addclip];//Get drawing contextCgcontextref context = Uigraphicsgetcurrentcontext ();//The context in which the picture is rendered[Self.imageView.layer Renderincontext:context];//Get pictures in contextUIImage *image = Uigraphicsgetimagefromcurrentimagecontext ();//Close bitmap contextUigraphicsendimagecontext ();//Remove the Cut Area view control and clear[Self.clipview Removefromsuperview]; Self.clipview = nil;//Display the picture on the ImageViewSelf.imageView.image = image;//Prompts the user via Alertview, whether to save the picture to the albumUialertview *alertview = [[Uialertview alloc] initwithtitle:@"Save Picture"message:@"Do you want to save the picture to an album?" "Delegate:self cancelbuttontitle:@"No"otherbuttontitles:@"Yes", nil]; [Alertview show];}}
Set the proxy method for Alertview to determine whether to save the picture
- (void)alertView:(nonnull UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ //若点击了“是”,则保存图片 if1) { UIImageWriteToSavedPhotosAlbum(self.imageView.image, nil, nil, nil); /** * 该方法可以设置保存完毕调用的方法,此处未进行设置 */ }}
The latest status of this blog will be synced to Sina Weibo account: secular island
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iosquartz2d-04-picture Trim and save to album