IOS Drawing Tutorial 2

Source: Internet
Author: User

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

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.