Content includes image watermark, picture clipping, screen, background tiling
1. Image watermark function
#import "Uiimage+mj.h" @implementation UIImage (MJ) + (Instancetype) WATERIMAGEWITHBG: (NSString *) BG Logo: (NSString *) logo{UIImage *bgimage = [UIImage IMAGENAMED:BG]; 1. Create a bitmap-based context (open a bitmap-based context) Uigraphicsbeginimagecontextwithoptions (bgimage.size, NO, 0.0); 2. Painting background [Bgimage drawinrect:cgrectmake (0, 0, BgImage.size.width, bgImage.size.height)]; 3. Draw the watermark in the lower right corner UIImage *waterimage = [UIImage Imagenamed:logo]; CGFloat scale = 0.2; CGFloat margin = 5; CGFloat WATERW = waterImage.size.width * scale; CGFloat waterh = waterImage.size.height * scale; CGFloat Waterx = Bgimage.size.width-waterw-margin; CGFloat watery = bgimage.size.height-waterh-margin; [Waterimage Drawinrect:cgrectmake (Waterx, watery, waterw, Waterh)]; 4. Obtain the finished UIImage object UIImage *newimage = Uigraphicsgetimagefromcurrentimagecontext () from the context; 5. End context Uigraphicsendimagecontext (); return newimage;} @endCall
1. Return watermark picture UIImage *newimage = [UIImage waterimagewithbg:@ "Scene" logo:@ "logo"]; 2. Display picture self.iconView.image = newimage;
2, picture cutting
#import "Uiimage+mj.h" @implementation UIImage (MJ) + (Instancetype) Circleimagewithname: (NSString *) name BorderWidth: ( CGFloat) borderWidth bordercolor: (Uicolor *) bordercolor{//1. Loading the original image UIImage *oldimage = [UIImage imagenamed:name]; 2. Open context CGFloat Imagew = oldImage.size.width + 2 * borderWidth; CGFloat Imageh = oldImage.size.height + 2 * borderWidth; Cgsize imageSize = Cgsizemake (Imagew, Imageh); Uigraphicsbeginimagecontextwithoptions (ImageSize, NO, 0.0); 3. Get the current context cgcontextref CTX = Uigraphicsgetcurrentcontext (); 4. Draw the frame (great circle) [BorderColor set]; CGFloat Bigradius = Imagew * 0.5; Circle radius cgfloat CenterX = Bigradius; Center CGFloat centery = Bigradius; Cgcontextaddarc (CTX, CenterX, CenterY, Bigradius, 0, M_PI * 2, 0); Cgcontextfillpath (CTX); Draw Circle//5. Small round cgfloat Smallradius = bigradius-borderwidth; Cgcontextaddarc (CTX, CenterX, CenterY, Smallradius, 0, M_PI * 2, 0); Cropping (the things that are drawn later are affected by clipping) Cgcontextclip (CTx); 6. Drawing [Oldimage Drawinrect:cgrectmake (BorderWidth, BorderWidth, OldImage.size.width, OldImage.size.height)]; 7. Take figure UIImage *newimage = Uigraphicsgetimagefromcurrentimagecontext (); 8. End context Uigraphicsendimagecontext (); return newimage;}@endCall
-(void) viewdidload{ [Super Viewdidload]; UIImage *newimage = [UIImage circleimagewithname:@ "Me" Borderwidth:3 bordercolor:[uicolor Whitecolor]]; Self.iconView.image = NewImage; NSData *data = uiimagepngrepresentation (newimage); NSString *path = [[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) LastObject] stringbyappendingpathcomponent:@ "New.png"]; [Data Writetofile:path atomically:yes];}
3. Screen
#import "Uiimage+mj.h" @implementation UIImage (MJ) + (Instancetype) Capturewithview: (UIView *) view{ //1. Open context uigraphicsbeginimagecontextwithoptions (view.frame.size, NO, 0.0); 2. Render the layer of the Controller view to the context [View.layer renderincontext:uigraphicsgetcurrentcontext ()]; 3. Remove the image UIImage *newimage = Uigraphicsgetimagefromcurrentimagecontext (); 4. End Context uigraphicsendimagecontext (); return newimage;} @end
Call
-(ibaction) clip { dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (1.0 * nsec_per_sec)), dispatch_get_ Main_queue (), ^{ //1. Capturing UIImage *newimage = [UIImage CaptureWithView:self.view]; 2. Write file nsdata *data = uiimagepngrepresentation (newimage); NSString *path = [[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) LastObject] stringbyappendingpathcomponent:@ "New.png"]; [Data Writetofile:path Atomically:yes]; });
4, background stretch
-(void) imagebg{ UIImage *oldimage = [UIImage imagenamed:@ "Me"]; Uigraphicsbeginimagecontextwithoptions (Self.view.frame.size, NO, 0.0); [Oldimage drawInRect:self.view.bounds]; UIImage *newimage = Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); Self.view.backgroundColor = [Uicolor colorwithpatternimage:newimage];}
5. Tile Background
-(void) viewdidload{[Super Viewdidload]; Self.view.backgroundColor = [Uicolor Redcolor]; 1. Create a row of background images cgfloat ROWW = self.view.frame.size.width;//cgfloat rowh = 40; CGFloat ROWH = 30; Uigraphicsbeginimagecontextwithoptions (Cgsizemake (ROWW, ROWH), NO, 0.0); Cgcontextref CTX = Uigraphicsgetcurrentcontext (); Draw a rectangular box [[Uicolor Redcolor] set]; Cgcontextaddrect (CTX, cgrectmake (0, 0, ROWW, ROWH)); Cgcontextfillpath (CTX); 2. Draw the line [[Uicolor Greencolor] set]; CGFloat linewidth = 2; Cgcontextsetlinewidth (CTX, linewidth); CGFloat Dividerx = 0; CGFloat dividery = rowh-linewidth; Cgcontextmovetopoint (CTX, Dividerx, dividery); Cgcontextaddlinetopoint (CTX, Roww-dividerx, dividery); Cgcontextstrokepath (CTX); 3. Take figure UIImage *newimage = Uigraphicsgetimagefromcurrentimagecontext (); 4. End context Uigraphicsendimagecontext (); 5. Set as Background Self.textView.backgroundColor = [Uicolor colorwithpatternImage:newimage];}