iOS Development UI Chapter-quartz2d use (draw basic graphics)

Source: Internet
Author: User

iOS Development UI Chapter-quartz2d use (draw basic graphics)

A simple explanation

Graphics context: is a cgcontextref type of data

The role of the graphics context: Saving drawing information, drawing state

Decide where to draw the output target (where to draw?) (The output target can be a PDF file, a bitmap, or a display window)

The same set of drawing sequences, specifying different graphics Context, can draw the same image to different targets.

The quartz2d provides several types of graphics Context:

Bitmap Graphics Context

PDF Graphics Context

Window Graphics Context

Layer Graphics Context

Printer Graphics Context

As long as the context is different, the place of drawing is different.

This article shows how to draw a picture onto the bitmap, which requires the creation of a picture that holds the drawing information.

Bitmap is a picture, equivalent to the uiimage of the system. A uiimage is a bitmap.

Second, how to draw the picture to the bitmap?

Note: You cannot get the bitmap context directly in the DrawRect: method, we need to create it ourselves.

code example:

 1//2//YYVIEWCONTROLLER.M 3//06-Draw Basic Graphics 4//5//Created by Apple on 14-6-22. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYViewController.h" @interface Yyviewcontroller () @property (weak, nonatomic) Iboutlet Uiimagev Iew *iv;13 @end14 @implementation YYViewController16-(void) viewDidLoad18 {[Super viewdidload];20 21//Plus Download Image 22//0. To create a bitmap context//c language method//Cgbitmapcontextcreate (< #void *data#>, < #size_t Width#>, &lt ; #size_t Height#>, < #size_t Bitspercomponent#>, < #size_t Bytesperrow#>, < #CGColorSpaceRef space# < #CGBitmapInfo bitmapinfo#>)//oc 26//Method 127//Uigraphicsbeginimagecontext (< #CGSize size     #>); 28//Method 229 Uigraphicsbeginimagecontextwithoptions (Cgsizemake (31), NO, 0); 30//1. Get Bitmap Context Cgcontextref CTX = Uigraphicsgetcurrentcontext (); 32//2. Draw (Draw a circle) cgcontextaddellipseinrect (CTX, CGRectMake (0, 0, 100, 100)); 34//3. Render Cgcontextstrokepath (CTX); 36//4. Get the generated picture UIImage *IMAGE=UIGRAPHICSGETIMAGEFROMCURRENTIMAGEC  Ontext (); 38//5. Displays the resulting picture to imageview39 self.iv.image=image;40//6. Save the drawn picture to the file 41//Convert the picture to binary data before writing the picture to the file 42 UIImageJPEGRepresentation (image, 1); The second parameter is the effect of the saved picture NSData *data=uiimagepngrepresentation (image); [Data writetofile:@]/users/apple/desktop/abc.pn G "atomically:yes];45}46-(void) didReceiveMemoryWarning48 {[Super didreceivememorywarning];50//Dispose O F any resources the can be recreated.51}52 @end

Program execution Effect:

When the program finishes executing, a abc.png picture is created at the specified location

Additional notes:

1. How to create a bitmap graphics context

Method 1 Uigraphicsbeginimagecontext (< #CGSize size#>);

Method 2 Uigraphicsbeginimagecontextwithoptions (cgsize size, BOOL opaque, cgfloat scale)

Using two methods can also be created, but using the first method to create future image clarity and quality is not as good as the second method. Method 2 receives three parameters:

Cgsize Size: Specifies how large the bitmap will be created in the future

BOOL opaque: Set transparent yes to represent transparency, no for opaque

CGFloat scale: Represents scaling, 0 for non-scaling

The created bitmap corresponds to a UIImage object

2.QUARTZ2D Memory Management

objects created with a function that contains "create" or "Copy" must be freed after use, or a memory leak will result

Object obtained with a function that does not contain "Create" or "Copy", you do not need to release

If an object is retain and is no longer in use, you need to release it

You can use the Quartz 2D function to specify retain and release an object. For example, if you create a Cgcolorspace object, use the functions Cgcolorspaceretain and cgcolorspacerelease to retain and release the object.

You can also use the Cfretain and cfrelease of the core Foundation. Note You cannot pass null values to these functions

iOS Development UI Chapter-quartz2d use (draw basic graphics)

Related Article

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.