IOS UIImage picture watermark, picture clipping, screenshot, background tiling

Source: Internet
Author: User

Image Watermark function

1 #import "Uiimage+zr.h"2 3 @implementationUIImage (ZR)4+ (Instancetype) WATERIMAGEWITHBG: (NSString *) BG Logo: (NSString *) Logo5 {6UIImage *bgimage =[UIImage IMAGENAMED:BG];7     8     //1. Create a bitmap-based context (open a bitmap-based context)9Uigraphicsbeginimagecontextwithoptions (Bgimage.size, NO,0.0);Ten      One     //2. Painting Background A[Bgimage Drawinrect:cgrectmake (0,0, BgImage.size.width, BgImage.size.height)]; -      -     //3. Draw the watermark in the lower right corner theUIImage *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; A [Waterimage drawinrect:cgrectmake (Waterx, watery, waterw, Waterh)]; at      -     //4. Get the finished UIImage object from the context -UIImage *newimage =Uigraphicsgetimagefromcurrentimagecontext (); -      -     //5. End Context - Uigraphicsendimagecontext (); in      -     returnNewImage; to } + @end

Picture clipping
1 #import "Uiimage+zr.h"2 3 @implementationUIImage (ZR)4+ (Instancetype) Circleimagewithname: (NSString *) name BorderWidth: (cgfloat) borderWidth bordercolor: (Uicolor *) bordercolor5 {6     //1. Loading the original7UIImage *oldimage =[UIImage imagenamed:name];8     9     //2. Open ContextTenCGFloat Imagew = OldImage.size.width +2*BorderWidth; OneCGFloat Imageh = OldImage.size.height +2*BorderWidth; ACgsize imageSize =Cgsizemake (Imagew, Imageh); -Uigraphicsbeginimagecontextwithoptions (ImageSize, NO,0.0); -      the     //3. Get the current context -Cgcontextref CTX =Uigraphicsgetcurrentcontext (); -      -     //4. Draw the border (great circle) +[BorderColorSet]; -CGFloat Bigradius = Imagew *0.5;//Circle Radius +CGFloat CenterX = Bigradius;//Center ACGFloat centery =Bigradius; atCgcontextaddarc (CTX, CenterX, CenterY, Bigradius,0, M_PI *2,0); -Cgcontextfillpath (CTX);//Draw a circle -      -     //5. Small Circle -CGFloat Smallradius = Bigradius-BorderWidth; -Cgcontextaddarc (CTX, CenterX, CenterY, Smallradius,0, M_PI *2,0); in     //cropping (the things that are drawn later will be affected by clipping) - Cgcontextclip (CTX); to      +     //6. Drawing - [Oldimage drawinrect:cgrectmake (BorderWidth, BorderWidth, OldImage.size.width, OldImage.size.height)]; the      *     //7. Take a picture $UIImage *newimage =Uigraphicsgetimagefromcurrentimagecontext ();Panax Notoginseng      -     //8. End Context the Uigraphicsendimagecontext (); +      A     returnNewImage; the } + @end

Screen

1 #import "Uiimage+zr.h"2 3 @implementationUIImage (ZR)4+ (Instancetype) Capturewithview: (UIView *) View5 {6     //1. Open Context7Uigraphicsbeginimagecontextwithoptions (View.frame.size, NO,0.0);8     9     //2. Render the layer of the Controller view to the contextTen [View.layer Renderincontext:uigraphicsgetcurrentcontext ()]; One      A     //3. Remove the image -UIImage *newimage =Uigraphicsgetimagefromcurrentimagecontext (); -      the     //4. End Context - Uigraphicsendimagecontext (); -      -     returnNewImage; + } - @end

Background stretch

1- (void) IMAGEBG2 {3UIImage *oldimage = [UIImage imagenamed:@"Me"];4     5Uigraphicsbeginimagecontextwithoptions (Self.view.frame.size, NO,0.0);6 [Oldimage drawInRect:self.view.bounds];7UIImage *newimage =Uigraphicsgetimagefromcurrentimagecontext ();8 Uigraphicsendimagecontext ();9     TenSelf.view.backgroundColor =[Uicolor colorwithpatternimage:newimage]; One}

Background tile

1- (void) Viewdidload2 {3 [Super Viewdidload];4     5 //Self.view.backgroundColor = [Uicolor redcolor];6     7     //1. Create a row of background images8CGFloat ROWW =Self.view.frame.size.width;9 //cgfloat rowh = +;TenCGFloat ROWH = -; OneUigraphicsbeginimagecontextwithoptions (Cgsizemake (ROWW, ROWH), NO,0.0); A      -Cgcontextref CTX =Uigraphicsgetcurrentcontext (); -     //Draw a rectangular box the[[Uicolor Redcolor]Set]; -Cgcontextaddrect (CTX, CGRectMake (0,0, ROWW, ROWH)); - Cgcontextfillpath (CTX); -      +     //2. Draw the line -[[Uicolor Greencolor]Set]; +CGFloat linewidth =2; A cgcontextsetlinewidth (CTX, linewidth); atCGFloat Dividerx =0; -CGFloat dividery = rowh-linewidth; - Cgcontextmovetopoint (CTX, Dividerx, dividery); -Cgcontextaddlinetopoint (CTX, ROWW-Dividerx, dividery); - Cgcontextstrokepath (CTX); -      in     //3. Take a picture -UIImage *newimage =Uigraphicsgetimagefromcurrentimagecontext (); to      +     //4. End Context - Uigraphicsendimagecontext (); the      *     //5. Set as Background $Self.textView.backgroundColor =[Uicolor colorwithpatternimage:newimage];Panax Notoginseng}

IOS UIImage picture watermark, picture clipping, screen, background tiling

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.