iOS Development _ Picture Doodle

Source: Internet
Author: User

Key code for the host controller:

A viewcontroller.m

#import "ViewController.h"#import "ZRPaintView.h"#import "Uiimage+zr.h"@interfaceViewcontroller ()-(ibaction) clear;-(ibaction) back;-(ibaction) Save; @property (weak, nonatomic) Iboutlet Zrpaintview*Paintview;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}-(Ibaction) Clear {[Self.paintview clear];}-(ibaction) Back {[Self.paintview back];}-(ibaction) Save {//1UIImage *image =[UIImage CaptureWithView:self.paintView]; //2 Save to pictureUiimagewritetosavedphotosalbum (image, Self, @selector (image:didFinishSavingWithError:contextInfo:), nil); }/** * * @param image Image * @param error error after saving picture operation*/- (voidImage: (UIImage *) image didfinishsavingwitherror: (nserror *) Error ContextInfo: (void*) contextinfo{if(Error) {NSLog (@"Save failed"); }Else{NSLog (@"saved successfully"); }}@end

Two create a subclass that inherits from UIView:

ZRPaintView.h

#import <UIKit/UIKit.h>@interface  zrpaintview:uiview-(void) Clear; -(void) back; @end

zrpaintview.m

#import "ZRPaintView.h"@interfaceZrpaintview () @property (nonatomic,strong) Nsmutablearray*totalpathpoints;//used to record@property (nonatomic,strong) Nsmutablearray *pathpoints;@end@implementationZrpaintview-(Nsmutablearray *) totalpathpoints{if(!_pathpoints) {_pathpoints=[Nsmutablearray array]; }    return_pathpoints;}-(void) clear{[self.totalpathpoints removeallobjects]; [Self setneedsdisplay];}-(void) back{[self.totalpathpoints Removelastobject]; [Self setneedsdisplay];}/** * Determine the starting point*/-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{Uitouch*touch =[touches anyobject]; Cgpoint startpos=[Touch LocationInView:touch.view]; //each time you start to touch, create a new array to hold a bit of this touch (all points in this touch process)Nsmutablearray *pathpoints =[Nsmutablearray array];    [PathPoints Addobject:[nsvalue Valuewithcgpoint:startpos];    [Self.totalpathpoints addobject:pathpoints]; //add all the points in this path to the array[self setneedsdisplay];}/** * Connection*/-(void) touchesmoved: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{Uitouch*touch =[touches anyobject]; Cgpoint Currentpos=[Touch LocationInView:touch.view]; //Take out the corresponding array of this pathNsmutablearray *pathpoints =[Self.totalpathpoints Lastobject];        [PathPoints Addobject:[nsvalue Valuewithcgpoint:currentpos]; [Self setneedsdisplay];}/** * Connection*/-(void) touchesended: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{//Uitouch *touch = [touches anyobject];//cgpoint endpos = [Touch LocationInView:touch.view];//    //    //Take out the corresponding array of this path//Nsmutablearray *pathpoints = [self.totalpathpoints lastobject];//[pathpoints addobject:[nsvalue valuewithcgpoint:endpos];//    //[self setneedsdisplay];[Self touchesmoved:touches withevent:Event];}- (void) DrawRect: (cgrect) rect {//Uirectfill (cgrectmake (0, 0, +));Cgcontextref CTX =Uigraphicsgetcurrentcontext ();  for(Nsmutablearray *pathpointsinchself.totalpathpoints) { for(inti =0; i<pathpoints.count; i++) {//a pathCgpoint pos =[pathpoints[i] cgpointvalue]; if(i==0) {cgcontextmovetopoint (CTX, pos.x, POS.Y); }Else{cgcontextaddlinetopoint (CTX, pos.x, POS.Y);    }}} cgcontextsetlinecap (CTX, Kcglinecapround);    Cgcontextsetlinejoin (CTX, Kcglinejoinround); Cgcontextsetlinewidth (CTX,5); Cgcontextstrokepath (CTX);}@end

Three create uiimage classification files:

Uiimage+zr.h

#import <UIKit/UIKit.h>@interface  UIImage (ZR)+ (Instancetype) Capturewithview: ( UIView *) view; @end

Uiimage+zr.m

#import "Uiimage+zr.h"@implementationUIImage (ZR)+ (Instancetype) Capturewithview: (UIView *) view{//1 Open ContextUigraphicsbeginimagecontextwithoptions (View.frame.size, NO,0.0); //2 render the layer of the Controller view to the context[View.layer Renderincontext:uigraphicsgetcurrentcontext ()]; //3 Take out the pictureUIImage *newimage =Uigraphicsgetimagefromcurrentimagecontext (); //4 End ContextUigraphicsendimagecontext (); returnnewimage;}@end

iOS Development _ Picture Doodle

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.