Cgcontextref Ciimageref Detailed

Source: Internet
Author: User

First Kind
Load a picture with the UIImage object first
And then convert it into cgimageref and put it in cgcontext to edit it.

The second Kind
Create cgimageref with the Cgimagecreate function
And put the cgimageref in the Cgcontext to edit it.

Third Kind
With Cgimagecreatecopy or Cgimagecreatecopywithcolorspace.
function copy

CGImageRef CGImageCreate (

   size_t width, //图片的宽度

   size_t height, //图片的高度

   size_t bitsPerComponent, //图片每个颜色的bits,比如rgb颜色空间,有可能是5 或者 8 ==

   size_t bitsPerPixel, //每一个像素占用的buts,15 位24位 32位等等

   size_t bytesPerRow, //每一行占用多少bytes 注意是bytes不是bits  1byte = 8bit

   CGColorSpaceRef colorspace, //颜色空间,比如rgb

   CGBitmapInfo bitmapInfo, //layout ,像素中bit的布局, 是rgba还是 argb,==

   CGDataProviderRef provider, //数据源提供者,url或者内存==

   const CGFloat decode[], //一个解码数组

   bool shouldInterpolate, //抗锯齿参数

   CGColorRenderingIntent intent //图片渲染相关参数

 }

One way to create an image mask
Cgimagemaskcreate

There are two ways to add mask to a picture,
First Kind
Use the function cgimagecreatewithmask or cgimagecreatewithmaskingcolors to mask the image directly.
This requires an alpha channel for the original picture, and if not, it will not have a translucent effect.

The second Kind
Use Cgcontextcliptomask to mask a rectangular area of the graph, so that you can achieve translucent results regardless of whether the original image has an alpha channel.

Bitmaps are always rectangular in shape

iphone supports image format:
JPEG, GIF, PNG, TIF, ICO, GMP, XBM, and CUR.

Create a picture that we need to provide something
A Bitmap Data source
An optional Decode array, which is applied to each pixel when rendered, is a color that changes to another color.
An interpolation setting, Boolean, whether to scale the picture when the interpolation algorithm is used, the specific algorithm does not know
Rendering intent
Picture size
Pixel Format: This includes 3 things,
1,bits per component, each of the component how many Bits
2. Bits per pixel, how many bits each pixel occupies
3. Bytes per row, how many Bytes each
Color Spaces and Bitmap Layout, here are a few things to provide
1,whether a bitmap contains an alpha channel. Whether the bitmap contains an alpha channel
2. Whether the color components have been multiplied by alpha value
3. Whether the data format is floating-point or integer
Bitmap layout refers to how the color components are specified
The following parameter bitmapinfo is Bitmap Layout

GImageRef CGImageCreate (

   size_t width,

   size_t height,

   size_t bitsPerComponent,

   size_t bitsPerPixel,

   size_t bytesPerRow,

   CGColorSpaceRef colorspace,

   CGBitmapInfo bitmapInfo,

   CGDataProviderRef provider,

   const CGFloat decode[],

   bool shouldInterpolate,

   CGColorRenderingIntent intent

);///Select a function to create an image

CGImageRef CGImageCreate (

   size_t width,

   size_t height,

   size_t bitsPerComponent,

   size_t bitsPerPixel,

   size_t bytesPerRow,

   CGColorSpaceRef colorspace,

   CGBitmapInfo bitmapInfo,

   CGDataProviderRef provider,

   const CGFloat decode[],

   bool shouldInterpolate,

   CGColorRenderingIntent intent

);  ///This is a more versatile function, but it also provides a lot of parameters.

Cgimageref Cgimagecreatewithjpegdataprovider (

   CGDataProviderRef source,

   const CGFloat decode[],

   bool shouldInterpolate,

   CGColorRenderingIntent intent

);         //Create an image from a JPEG source

Cgimageref Cgimagecreatewithpngdataprovider (

   CGDataProviderRef source,

   const CGFloat decode[],

   bool shouldInterpolate,

   CGColorRenderingIntent intent

);     //Create an image from a PNG source

CGImageRef CGImageCreateWithImageInRect (

   CGImageRef image,

   CGRect rect

);     //Create a picture from an area of a picture, like

CGImageRef CGImageSourceCreateImageAtIndex (

   CGImageSourceRef isrc,

   size_t index,

   CFDictionaryRef options

);     //Create a picture from a picture source, which may contain more than one picture, 0 means the first one

CGImageRef CGImageSourceCreateThumbnailAtIndex (

   CGImageSourceRef isrc,

   size_t index,

   CFDictionaryRef options

);     //Create a thumbnail from a picture source, the source of the image may contain more than one picture, 0 means the first

CGImageRef CGBitmapContextCreateImage (

   CGContextRef c

);     //Create a picture from a graphical context

CGImageRef CGImageCreateCopy (

   CGImageRef image

);     //Copy a picture

CGImageRef CGImageCreateCopyWithColorSpace (

   CGImageRef image,

   CGColorSpaceRef colorspace

);     //Copy

A picture that replaces the original color space

Create a picture from a JPEG

void MyCreateAndDrawBitmapImage (CGContextRef myContext,

                                CGRect myContextRect,

                                const char *filename);

{

    CGImageRef image;

    CGDataProviderRef provider;

    CFStringRef path;

    CFURLRef url;

    path = CFStringCreateWithCString (NULL, filename,

                        kCFStringEncodingUTF8);

    url = CFURLCreateWithFileSystemPath (NULL, path, // 2

                            kCFURLPOSIXPathStyle, NO);

    CFRelease(path);

    provider = CGDataProviderCreateWithURL (url);// 3

    CFRelease (url);

    image = CGImageCreateWithJPEGDataProvider (provider,// 4

                                    NULL,

                                    true,

                                    kCGRenderingIntentDefault);

    CGDataProviderRelease (provider);// 5

    CGContextDrawImage (myContext, myContextRect, image);// 6

    CGImageRelease (image);// 7

}

CGImageRef CGImageMaskCreate (

        size_t width,

        size_t height,

        size_t bitsPerComponent,

        size_t bitsPerPixel,

        size_t bytesPerRow,

        CGDataProviderRef provider,

        const float decode[],

        int shouldInterpolate

);  

//Create a cglayerref from a graphical context that already exists

CGLayerRef CGLayerCreateWithContext (

   CGContextRef context,

   CGSize size,

   CFDictionaryRef auxiliaryInfo

);

CGSize CGLayerGetSize (

   CGLayerRef layer

);

And then get cgcontextref from the Cglayerref, that's the graphics context.

CGContextRef CGLayerGetContext (

   CGLayerRef layer

);

And then painting in this cgcontextref.

Draw the CGLayer to the Destination Graphics Context

void CGContextDrawLayerInRect (

   CGContextRef context,

   CGRect rect,

   CGLayerRef layer

);

void CGContextDrawLayerAtPoint (

   CGContextRef context,

   CGPoint point,

   CGLayerRef layer

);

Transparency Layers Shadow
Transparency Layers gives us a very handy shade.

void Cgcontextbegintransparencylayer (Cgcontextref context, cfdictionaryref auxiliaryinfo);

The second parameter of the function can pass some set parameters, and no null is passed
Look at the sample code below

CGSize myShadowOffset = CGSizeMake (10, -20);// 设置阴影的偏移量

CGContextSetShadow (myContext, myShadowOffset, 5); // 第三个参数表示透明和模糊度

CGContextBeginTransparencyLayer (myContext, NULL);

CGContextSetRGBFillColor (myContext, 0, 1, 0, 1);

CGContextFillRect (myContext, CGRectMake (wd/3+ 50,ht/2 ,wd/4,ht/4));

CGContextEndTransparencyLayer (myContext);

CGContextBeginTransparencyLayer (myContext, NULL);// 4

CGContextSetRGBFillColor (myContext, 0, 0, 1, 1);

CGContextFillRect (myContext, CGRectMake (wd/3-50,ht/2-100,wd/4,ht/4));

CGContextSetRGBFillColor (myContext, 1, 0, 0, 1);

CGContextFillRect (myContext, CGRectMake (wd/3,ht/2-50,wd/4,ht/4));

CGContextEndTransparencyLayer (myContext);

Cgcontextref Ciimageref Detailed

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.