UIImage provides a variety of ways to draw bitmaps, in addition to the previously used Drawinrect, we can also use the following methods
Drawatpoint: Need to provide cgpoint the starting point of the mapping in Cgcontextref
DrawAtPoint:blendMode:alpha: The following two parameters specify the overlay mode of the picture and the transparency
DrawInRect:blendMode:alpha: The following two parameters specify the overlay mode of the picture and the transparency
In addition, we can draw bitmaps using the methods in the core graphics
void Cgcontextdrawimage (CGCONTEXTREF,CGRECT,CGIMAGEREF);
void Cgcontextdrawtiledimage (Cgcontextref,cgrect,cgimageref); This method draws the picture into a rect area in tiled mode
If you need to get all or part of an existing picture, you can use the following methods
Cgimageref cgimagecreatecopy (CGIMAGEREF);
Cgimageref Cgimagecreatewithimageinrect (Cgimageref,cgrect);
We can add a watermark to a picture by drawing a bitmap
@implementationZltview {UIImage*_image;}- (ID) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {[self drawImage]; } returnSelf ;}- (void) drawImage {uigraphicsbeginimagecontext (self.frame.size); Cgcontextref Context=Uigraphicsgetcurrentcontext (); Cgcontextsavegstate (context); //The coordinate origin of the coordinate system in the core graphics is in the lower-left corner of the screen, and the square is up along the y-axis, as opposed to uikit, so a function that uses the core graphic directly draws the image with the y-axis reversed//first the y-axis is scaled-1, which is equivalent to rotating along X-sheetsCGCONTEXTSCALECTM (Context,1, -1); //y-axis panning to move the origin to the upper-left cornerCGCONTEXTTRANSLATECTM (Context,0, -self.frame.size.height); Cgcontextdrawimage (context, Self.frame, [UIImage imagenamed:@"girl.jpg"]. Cgimage); //the drawing environment before replyingcgcontextrestoregstate (context); //plotting with Uikit does not require a change of proof because Uikit is processed[@"made by Zlt"Drawatpoint:cgpointmake (0, Self.frame.size.height- +) Withattributes:@{nsforegroundcolorattributename:[uicolor Greencolor],nsfontattributename:[uifont FontWithName:@"Arial"Size -]}]; _image=Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext ();}- (void) DrawRect: (cgrect) rect{[_image Drawatpoint:cgpointzero];}
The following example creates a taxonomy for UIImage, with the following features:
+ (UIImage *) Captureview: (UIView *) TargetView; Get a screenshot of UIView
+ (UIImage *) capturescreent; Get screen Screenshots
-(UIImage *) Imageatrect: (cgrect) rect; Get part of an existing uiimage
-(UIImage *) imageaspectbyminsize: (cgsize) targersize; Scales the uiimage proportionally to the short edge of the provided area
-(UIImage *) imageaspectbymaxsize: (cgsize) targersize; Scales the uiimage proportionally to the long edge of the provided area
-(UIImage *) imagerotate: (cgfloat) radians; Rotate the UIImage
+ (UIImage *) Captureview: (UIView *) TargetView {uigraphicsbeginimagecontext (targetView.frame.size); Cgcontextref Context=Uigraphicsgetcurrentcontext (); [Targetview.layer Renderincontext:context]; UIImage*image =Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); returnimage;}+ (UIImage *) capturescreent {//private functions can also intercept the root UIView externcgimageref uigetscreenimage (); UIImage*image =[UIImage imagewithcgimage:uigetscreenimage ()]; returnimage;}-(UIImage *) Imageatrect: (cgrect) rect {cgimageref Imgref=Cgimagecreatewithimageinrect ([self cgimage], rect); UIImage*image =[UIImage Imagewithcgimage:imgref]; returnimage;}-(UIImage *) Imageaspectbyminsize: (cgsize) targersize {if(!cgsizeequaltosize (Self.size, targersize)) {CGFloat XFactor= Targersize.width/Self.size.width; CGFloat YFACOTR= Targersize.height/Self.size.height; CGFloat Factor= XFactor < YFACOTR?XFACTOR:YFACOTR; CGFloat nwidth= Self.size.width *factor; CGFloat nheight= Self.size.height *factor; CGRect rect; if(XFactor <YFACOTR) {Rect= CGRectMake (0, (Targersize.height-nheight)/2, nwidth, nheight); } Else{rect= CGRectMake ((targersize.width-nwidth)/2,0, nwidth, nheight); } uigraphicsbeginimagecontext (Targersize); [Self drawinrect:rect]; UIImage*image =Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); returnimage; } returnSelf ;}-(UIImage *) Imageaspectbymaxsize: (cgsize) targersize {if(!cgsizeequaltosize (self.size,targersize)) {CGFloat XFactor= Targersize.width/Self.size.width; CGFloat Yfactor= Targersize.height/Self.size.height; CGFloat Factor= XFactor > Yfactor?Xfactor:yfactor; CGFloat nwidth= Self.size.width *factor; CGFloat nheight= Self.size.height *factor; CGRect rect=Cgrectzero; if(XFactor >yfactor) {Rect= CGRectMake (0,-(Nheight-targersize.height)/2, nwidth, nheight); } Else{rect= CGRectMake (-(nwidth-targersize.width)/2,0, nwidth, nheight); } uigraphicsbeginimagecontext (Targersize); [Self drawinrect:rect]; UIImage*image =Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); returnimage; } returnSelf ;}-(UIImage *) Imagerotate: (cgfloat) radians {//gets the rectangular area after rotationCGRect rect = Cgrectapplyaffinetransform (CGRectMake (0,0, Self.size.width, Self.size.height), cgaffinetransformmakerotation (radians)); Uigraphicsbeginimagecontext (rect.size); Cgcontextref Context=Uigraphicsgetcurrentcontext (); //moves the center of coordinates to the center of the picture, which rotates the picture around the centerCGCONTEXTTRANSLATECTM (Context, rect.size.width/2, rect.size.height/2); CGCONTEXTROTATECTM (context, radians); CGCONTEXTSCALECTM (Context,1, -1); Cgcontextdrawimage (Context, CGRectMake (-rect.size.width/2,-rect.size.height/2, Rect.size.width, rect.size.height), self. Cgimage); UIImage*nimage =Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); returnnImage;}
iOS image processing (7) Drawing bitmaps