1. Image Watermark
The layer context can only be displayed in DrawRect.
When the context is turned on, drawing can be done in viewdidload
Context of the bitmap
Uigraphicsbeginimagecontext () for non-retina screens only
Open Bitmap Context
Size: Dimensions of the bitmap
Opaque: Opaque is yes, transparency is no
Scale: Whether the context is scaled
Uigraphicsbeginimagecontextwithoptions (image.size, NO, 0);
Draw the original picture
[Image Drawatpoint:cgpointzero];
[Image drawinrect:cgrectmake (0, 0, Image.size.width, image.size.height)];
First Kind
NSString * info = @ "@ Big Huan";
Nsdictionary * dict = @{nsforegroundcolorattributename:[uicolor Whitecolor],
Nsfontattributename:[uifont systemfontofsize:30]};
[Info Drawatpoint:cgpointmake (+) withattributes:dict];
The second Kind
Uibezierpath * Path = [Uibezierpath bezierpathwithrect:cgrectmake (100, 100, 100, 100)];
[[Uicolor Redcolor] set];
[Path fill];
Third Kind
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
Cgcontextmovetopoint (CTX, 20, 20);
Cgcontextaddlinetopoint (CTX, 40, 40);
Cgcontextstrokepath (CTX);
Generate a new picture, obtained from the bitmap context
UIImage * newimage = Uigraphicsgetimagefromcurrentimagecontext ();
Close Picture Context
Uigraphicsendimagecontext ();
Self.imageView.image = NewImage;
2, picture and other proportion of the reduction
Action write before getting a picture through context
UIImage * image = [UIImage imagenamed:@ "huoyanshan.jpg"];
CGFloat NEWIMAGEWH = image.size.height;
Open Bitmap Context
Uigraphicsbeginimagecontextwithoptions (Cgsizemake (NEWIMAGEWH, NEWIMAGEWH), NO, 0);
Uibezierpath * Path = [Uibezierpath bezierpathwithovalinrect:cgrectmake (0, 0, NEWIMAGEWH, NEWIMAGEWH)];
Add cut
[Path Addclip];
Drawing
[Image Drawatpoint:cgpointzero];
Get a picture by context
UIImage * newimage = Uigraphicsgetimagefromcurrentimagecontext ();
Close context
Uigraphicsendimagecontext ();
Self.imageView.image = NewImage;
3. Draw the Line
The first method of pure code execution
-(Instancetype) initWithFrame: (CGRect) frame
{
self = [super Initwithframe:frame];
if (self) {
[Self setUp];
}
return self;
}
The first method of storyboard execution
-(void) awakefromnib {
[Self setUp];
}
4. Albums
Save to Phone album
Uiimagewritetosavedphotosalbum (image, Self, @selector (image:didFinishSavingWithError:contextInfo:), nil);
Create albums
Uiimagepickercontroller * Imagepicker = [[Uiimagepickercontroller alloc] init];
<uinavigationcontrollerdelegate, Uiimagepickercontrollerdelegate>, write two protocols.
Imagepicker.delegate = self;
Uiimagepickercontrollersourcetypephotolibrary albums
//uiimagepickercontrollersourcetypecamera Camera
Imagepicker.sourcetype = uiimagepickercontrollersourcetypephotolibrary;
Allow Editing
imagepicker.allowsediting = YES;
[Self Presentviewcontroller:imagepicker animated:yes completion:nil];
}
How to choose a photo album
-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary< NSString *,id> *) Info {
Edit picture if allowsediting has no value for No,image
_drawview.image = Info[uiimagepickercontrollereditedimage];
Original picture
_drawview.image = Info[uiimagepickercontrolleroriginalimage];
[Picker Dismissviewcontrolleranimated:yes Completion:nil];
}
Cancel Selection
-(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) Picker {
NSLog (@ "Cancel selection");
[Picker Dismissviewcontrolleranimated:yes Completion:nil];
}
IOS Drawing Tutorial 2