iOS basic graphics are drawn based on bitmap bitmaps

Source: Internet
Author: User

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;} @end
Call

     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;}@end
Call

-(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];} 




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.