In the inheritance UIView create Pidrawerview class,. m affirms an enumeration type Ns_enum,set with Get line type DrawingMode and color settings uicolor
#import <UIKit/UIKit.h>
typedef Ns_enum(Nsinteger, DrawingMode) {
Drawingmodenone = 0,
Drawingmodepaint,
Drawingmodeerase,
};
@interface Pidrawerview:uiview
@property (nonatomic, ReadWrite) DrawingMode DrawingMode;
@property (nonatomic, strong) Uicolor *selectedcolor;
@end
Implementing the declaration in the inheritance UIView.h file
#import "PIDrawerView.h"
@interface Pidrawerview ()
{
Cgpoint Previouspoint;
Cgpoint Currentpoint;
}
@property (nonatomic, Strong) UIImage * viewimage;
@end
@implementation Pidrawerview
-(void) awakefromnib
{
[Self initialize];
}
-(void) DrawRect: (cgrect) rect
{
[Self.viewimage DrawInRect:self.bounds];
}
#pragma mark-setter methods
-(void) Setdrawingmode: (DrawingMode) DrawingMode
{
_drawingmode = DrawingMode;
}
#pragma mark-private methods
-(void) Initialize
{
Currentpoint = cgpointmake (0, 0);
Previouspoint = Currentpoint;
_drawingmode = Drawingmodenone;
_selectedcolor = [Uicolor blackcolor];
}
-(void) eraseline
{
Uigraphicsbeginimagecontext (self.bounds.size);
[Self.viewimage DrawInRect:self.bounds];
Cgcontextsetblendmode (Uigraphicsgetcurrentcontext (), kcgblendmodeclear);
Cgcontextsetlinecap (Uigraphicsgetcurrentcontext (), kcglinecapround);
Cgcontextsetlinewidth (Uigraphicsgetcurrentcontext (), 10);
Cgcontextbeginpath (Uigraphicsgetcurrentcontext ());
Cgcontextsetblendmode (Uigraphicsgetcurrentcontext (), kcgblendmodeclear);
Cgcontextmovetopoint (Uigraphicsgetcurrentcontext (), Previouspoint.x, PREVIOUSPOINT.Y);
Cgcontextaddlinetopoint (Uigraphicsgetcurrentcontext (), Currentpoint.x, CURRENTPOINT.Y);
Cgcontextstrokepath (Uigraphicsgetcurrentcontext ());
Self.viewimage = Uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();
Previouspoint = Currentpoint;
[Self setneedsdisplay];
}
-(void) drawlinenew
{
Uigraphicsbeginimagecontext (self.bounds.size);
[Self.viewimage DrawInRect:self.bounds];
Cgcontextsetlinecap (Uigraphicsgetcurrentcontext (), kcglinecapround);
Cgcontextsetstrokecolorwithcolor (Uigraphicsgetcurrentcontext (), self.selectedColor.CGColor);
Cgcontextsetlinewidth (Uigraphicsgetcurrentcontext (), 10);
Cgcontextbeginpath (Uigraphicsgetcurrentcontext ());
Cgcontextmovetopoint (Uigraphicsgetcurrentcontext (), Previouspoint.x, PREVIOUSPOINT.Y);
Cgcontextaddlinetopoint (Uigraphicsgetcurrentcontext (), Currentpoint.x, CURRENTPOINT.Y);
Cgcontextstrokepath (Uigraphicsgetcurrentcontext ());
Self.viewimage = Uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();
Previouspoint = Currentpoint;
[Self setneedsdisplay];
}
-(void) handletouches
{
if (Self.drawingmode = = Drawingmodenone) {
Do nothing
}
else if (Self.drawingmode = = Drawingmodepaint) {
[Self drawlinenew];
}
Else
{
[Self eraseline];
}
}
#pragma mark-touches methods
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
Cgpoint p = [[Touches anyobject] locationinview:self];
Previouspoint = p;
}
-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event
{
Currentpoint = [[Touches anyobject] locationinview:self];
[Self handletouches];
}
-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event
{
Currentpoint = [[Touches anyobject] locationinview:self];
[Self handletouches];
}
Introduction of Pidrawerview class and mobilecoreservices/mobilecoreservices library files in Viewcontroller
#import "ViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "PIDrawerView.h"
@interface Viewcontroller () <uiactionsheetdelegate,uiimagepickercontrollerdelegate, Uinavigationcontrollerdelegate>
{
Pidrawerview * VIEWDR;
UIView * Viewimageview;
}
@property (nonatomic, strong) Uicolor *selectedcolor;
@property (nonatomic) Uiimageview * Backgroundimageview;
@end
@implementation Viewcontroller
-(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) Nibbundleornil
{
self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil];
if (self) {
Custom initialization
}
return self;
}
-(void) Viewdidload {
[Super Viewdidload];
Viewimageview = [[UIView alloc]initwithframe:cgrectmake (0, 0, self.view.frame.size.width-20, 350)];
[Self.view Addsubview:viewimageview];
_backgroundimageview = [[Uiimageview alloc]initwithframe:viewimageview.bounds];
_backgroundimageview.userinteractionenabled = YES;
VIEWDR = [[Pidrawerview alloc]initwithframe:viewimageview.bounds];
viewdr.userinteractionenabled = YES;
Viewdr.backgroundcolor = [Uicolor Clearcolor];
[Viewimageview Addsubview:_backgroundimageview];
[Viewimageview ADDSUBVIEW:VIEWDR];
Self.selectedcolor = [Uicolor blackcolor];
Start drawing lines
[VIEWDR SetSelectedColor:self.selectedColor];
[VIEWDR SetSelectedColor:self.selectedColor];
UIView * Viewhight = [[UIView alloc]initwithframe:cgrectmake (1, self.view.frame.size.height-50, Self.view.frame.size.width, 100)];
[Self.view Addsubview:viewhight];
UIButton * Buttonq = [[UIButton alloc]initwithframe:cgrectmake (20, 0, 150, 50)];
[Buttonq settitle:@ "edit smear" forstate:uicontrolstatenormal];
[Buttonq addtarget:self Action: @selector (ONLICK2) forcontrolevents:uicontroleventtouchupinside];
[Buttonq Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];
[Viewhight Addsubview:buttonq];
UIButton * button = [[UIButton alloc]initwithframe:cgrectmake (150, 0, 150, 50)];
[Button settitle:@ "into the album" Forstate:uicontrolstatenormal];
[Button addtarget:self action: @selector (Onlick) forcontrolevents:uicontroleventtouchupinside];
[Button Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];
[Viewhight Addsubview:button];
UIButton * Buttonsave = [[UIButton alloc]initwithframe:cgrectmake (350, 0, 50, 50)];
[Buttonsave settitle:@ "screenshot" Forstate:uicontrolstatenormal];
[Buttonsave Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];
[Buttonsave addtarget:self Action: @selector (Saveonlick) forcontrolevents:uicontroleventtouchupinside];
[Viewhight Addsubview:buttonsave];
}
Save
-(void) saveonlick{
Start screenshot
Uigraphicsbeginimagecontextwithoptions (ViewimageView.bounds.size, NO, 2.0);
Path
Cgcontextref context = Uigraphicsgetcurrentcontext ();
//
[Self.view.layer Renderincontext:context];
Image
UIImage *image = Uigraphicsgetimagefromcurrentimagecontext ();
Uiimagewritetosavedphotosalbum (image, Self, @selector (image:didFinishSavingWithError:contextInfo:), nil);
}
-(void) Image: (UIImage *) image didfinishsavingwitherror: (nserror *) error ContextInfo: (void *) contextinfo{
if (Error = = nil) {
Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Prompt" message:@ "saved to Mobile album" Delegate:self Cancelbuttontitle:nil otherbuttontitles:@ "OK", nil];
[Alert show];
}else{
Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Prompt" message:@ "Save Failed" Delegate:self Cancelbuttontitle:nil otherbuttontitles:@ "OK", nil];
[Alert show];
}
}
To underline
-(void) onlick2{
[VIEWDR Setdrawingmode:drawingmodepaint];
}
Enter album
-(void) onlick{
Uiactionsheet *actionsheet = [[Uiactionsheet alloc] initwithtitle:@ "Pick" delegate:self cancelbuttontitle:@ "Cancel" Destructivebuttontitle:nil otherbuttontitles:@ "Pick from Library", nil];
[Actionsheet ShowInView:self.view];
}
#pragma mark-uiactionsheetdelegate methods
-(void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (Nsinteger) Buttonindex
{
Uiimagepickercontroller *mediaui = [[Uiimagepickercontroller alloc] init];
Mediaui.sourcetype = uiimagepickercontrollersourcetypephotolibrary;
Mediaui.mediatypes = [Uiimagepickercontroller availablemediatypesforsourcetype: Uiimagepickercontrollersourcetypesavedphotosalbum];
mediaui.allowsediting = NO;
Mediaui.delegate = self;
[Self Presentviewcontroller:mediaui animated:yes completion:nil];
}
#pragma mark-uiimagepickercontrollerdelegate methods
-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary *) info
{
UIImage *image = nil;
if (Picker.sourcetype = = Uiimagepickercontrollersourcetypecamera)
{
image = [info objectforkey:uiimagepickercontrolleroriginalimage];
}
Else
{
NSString *mediatype = [info objectforkey:uiimagepickercontrollermediatype];
if (Cfstringcompare (CFSTRINGREF) mediatype, kuttypeimage, 0) = = Kcfcompareequalto)
{
Image = (UIImage *) [info objectforkey:uiimagepickercontrolleroriginalimage];
}
}
Self.backGroundImageView.image = image;
[Picker Dismissviewcontrolleranimated:yes Completion:nil];
}
uiactionsheet--photos from the album to smear, save to the album demo completed