Learn about iPhone dual-BUFFER MECHANISM

Source: Internet
Author: User

IPhone dual BufferingMechanism is the content to be introduced in this article. I believe most people know that the so-called "ScreenDouble Buffering"Is to create a" graphics device context cache "in the memory, and all the drawing operations are performed on this" graphics context cache, update the image context to the screen device again.

The iPhone platform provides the following APIs:

 
 
  1. CGContextRef CGBitmapContextCreate (  
  2.    void *data,  
  3.    size_t width,  
  4.    size_t height,  
  5.    size_t bitsPerComponent,  
  6.    size_t bytesPerRow,  
  7.    CGColorSpaceRef colorspace,  
  8.    CGBitmapInfo bitmapInfo  
  9. ); 

The meanings of each API parameter are as follows:

The parameter data points to the memory area rendered by the drawing operation. The memory area size should be bytesPerRow * height) bytes. If you do not have any special requirements for the rendering memory area, you can pass NULL to the date parameter.

The width parameter indicates the width of the rendered memory area.

The height parameter indicates the height of the rendered memory area.

The bitsPerComponent parameter is used to render the bits of components in the memory area on each pixel of the screen. For example, if the 32-bit pixel and RGB color format are used, in the RGBA color format, the bits required for each component on each pixel of the screen is 32/4 = 8.

The bytesPerRow parameter indicates the number of bytes used by each row in the rendered memory area.

The colorspace parameter is used for the bitmap context in the rendered memory area ".

The bitmapInfo parameter specifies whether the "View" of the rendered memory area contains an alpha perspective) channel and the corresponding position of each pixel. In addition, you can also specify whether the component type is a floating point value or an integer value.

From the interface definition, we can see that when calling this function, the system will create a "view rendering environment", which is a "view context" defined by the reader ". When the reader draws in this "view context", the system will render the rendering operation into bitmap data in the defined Rendering Memory area. The pixel format of "view context" is defined by three parameters, namely the bits, colorspace, and alpha perspective occupied by each component. The alpha value specifies the opacity of each pixel.

Based on the knowledge points described above, the author defines the rendered memory area as follows:

 
 
  1. imageData = malloc((iFrame.size.width)*(iFrame.size.height)*32); 

The 32-bits is used to represent the RGBA color format in each pixel on the screen. The bitsPerComponent parameter is 32/4 = 8. The parameters are defined as follows:

 
 
  1. iDevice = CGBitmapContextCreate(imageData,iFrame.size.width,  
  2. iFrame.size.height,8,32*(iFrame.size.width),iColorSpace,kCGImageAlphaPremultipliedLast); 

Here, the method for getting iColorSpace is as follows:

 
 
  1. iColorSpace = CGColorSpaceCreateDeviceRGB();  
  2. CGColorSpaceCreateDeviceRGB() 

You can obtain the RGB Color Space unrelated to the device. The color space needs to be released by calling CGColorSpaceRelease.

After the view context "iDevice" is created for the successfully rendered memory area, the reader can draw the map context in the rendered memory area, as mentioned above, all the painting operations will be rendered as bitmap data in the rendered memory area. The painting operations are as follows:

 
 
  1. // Draw an image
  2. CGContextDrawImage (iDevice, CGRectMake (0, 0, iFrame. size. width, iFrame. size. height), aImage );
  3. // Draw a translucent rectangle
  4. CGRect rt;
  5. Rt. origin. x = 100;
  6. Rt. origin. y = 20;
  7. Rt. size. width = 200;
  8. Rt. size. height = 200;
  9. CGContextSaveGState (iDevice );
  10. CGContextSetRGBFillColor (iDevice, 1.0, 1.0, 1.0, 0.5 );
  11. CGContextFillRect (iDevice, rt );
  12. CGContextRestoreGState (iDevice );
  13. CGContextStrokePath (iDevice );
  14. // Draw a straight line
  15. CGContextSetRGBStrokeColor (iDevice, 1.0, 0.0, 0.0, 1.0 );
  16. CGPoint pt0, pt1;
  17. CGPoint points [2];
  18. Pt0.x = 10;
  19. Pt0.y = 250;
  20. Pt1.x = 310;
  21. Pt1.y = 250;
  22. Points [0] = pt0;
  23. Points [1] = pt1;
  24. CGContextAddLines (iDevice, points, 2 );
  25. CGContextStrokePath (iDevice );

It can be seen that in the "bitmap context" of the rendered memory area, various painting operations such as images, rectangles, and straight lines can be performed. These operations are rendered as bitmap data, you can obtain the rendered "bitmap" using the following method ":

 
 
  1. -(void)drawRect:(CGRect)rect {  
  2.     // Drawing code  
  3.          UIGraphicsGetCurrentContext();        
  4.          UIImage* iImage = [UIImage imageNamed:@"merry.png"];  
  5.          [iOffScreenBitmap DrawImage:iImage.CGImage];  
  6.          UIImage* iImage_1 = [UIImage imageWithCGImage:[iOffScreenBitmap Gc]];  
  7.          [iImage_1 drawInRect:CGRectMake(0, 0, 120, 160)];  

In the above Code, use the DrawImage: cgimageref of iOffScreenBitmap to draw the image merry.png to the screen.Double BufferingAnd then draw a rectangle and a straight line. Then, use the CGBitmapContextCreateImage: CGConotextRef method to obtain"ViewContextViewSnapshot) "image_1, and finally put this"ViewThe snapshot is updated to the screen to implement the screenDouble BufferingThe results are as follows:

SummaryIPhone dual BufferingThis article is helpful to you!

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.