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

Source: Internet
Author: User

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.M3 //06-Draw basic Graphics4 //5 //Created by Apple on 14-6-22.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 #import "YYViewController.h"Ten  One @interfaceYyviewcontroller () A@property (Weak, nonatomic) Iboutlet Uiimageview *IV; - @end -  the @implementationYyviewcontroller -  -- (void) Viewdidload - { + [Super Viewdidload]; -  +     //Loading Pictures A     //0. Create a bitmap context at     //method of C language - //Cgbitmapcontextcreate (< #void *data#>, < #size_t Width#>, < #size_t Height#>, < #size_t Bitsperc Omponent#>, < #size_t Bytesperrow#>, < #CGColorSpaceRef Space#>, < #CGBitmapInfo bitmapinfo#>)  -     //methods of encapsulation in OC -     //Method 1 - //Uigraphicsbeginimagecontext (< #CGSize size#>); -     //Method 2 inUigraphicsbeginimagecontextwithoptions (Cgsizemake ( $, $), NO,0); -     //1. Get the bitmap context toCgcontextref CTX =Uigraphicsgetcurrentcontext (); +     //2. Drawing (Draw a circle) -Cgcontextaddellipseinrect (CTX, CGRectMake (0,0, -, -)); the     //3. Rendering * Cgcontextstrokepath (CTX); $     //4. Get the generated picturePanax NotoginsengUIImage *image=Uigraphicsgetimagefromcurrentimagecontext (); -     //5. Show the generated image to ImageView theSelf.iv.image=image; +     //6. Save the drawn picture to the file A     //first convert the picture to binary data, and then write the picture to the file the //uiimagejpegrepresentation (image, 1);//The second argument is the effect of a saved picture +NSData *data=uiimagepngrepresentation (image); -[Data writetofile:@"/users/apple/desktop/abc.png"Atomically:yes]; $ } $  -- (void) didreceivememorywarning - { the [Super didreceivememorywarning]; -     //Dispose of any resources the can be recreated.Wuyi } the  - @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 for opaque, no for transparent

CGFloat scale: Represents scaling, 0 means the system automatically sets the scaling factor based on the screen factor of the current device

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)

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.