UIGraphicsBeginImageContextWithOptions用法(UIView 转 UIImage)


UIView 转 UIImage 方法如下(关键是自适配分辨率):


- (UIImage*)lineImage:(UIColor *)color lineWidth:(float)width lineType:(int)type

{

    self.lineColor = color;

    lineWidth = width;

    lineType = type;

    

    //opaqueNO 不透明

    UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 1.0);

//渲染自身

    [self.layer renderInContext:UIGraphicsGetCurrentContext()];

    

    UIImage *uiImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

//    [UIImagePNGRepresentation(uiImage) writeToFile:@"/users/test/desktop/a.png" atomically:YES]; 

    

    return uiImage;

}



UIGraphicsBeginImageContextWithOptions

Creates a bitmap-based graphics context with the specified options.

void UIGraphicsBeginImageContextWithOptions(
   CGSize size,
   BOOL opaque,
   CGFloat scale
);
Parameters
size

The size (measured in points) of the new bitmap context. This represents the size of the image returned by the UIGraphicsGetImageFromCurrentImageContext function. To get the size of the bitmap in pixels, you must multiply the width and height values by the value in the scale parameter.

opaque -- 不透明(YES)

A Boolean flag indicating whether the bitmap is opaque. If you know the bitmap is fully opaque, you can specify YES for this parameter to optimize the bitmap storage. Specifying NO means that the bitmap must include an alpha channel to handle any partially transparent pixels.

scale

The scale factor to apply to the bitmap. If you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen.


 

Updating Your Custom Drawing Code

When you do any custom drawing in your application, most of the time you should not need to care about the resolution of the underlying screen. The native drawing technologies automatically ensure that the coordinates you specify in the logical coordinate space map correctly to pixels on the underlying screen. Sometimes, however, you might need to know what the current scale factor is in order to render your content correctly. For those situations, UIKit, Core Animation, and other system frameworks provide the help you need to do your drawing correctly.

Creating High-Resolution Bitmap Images Programmatically If you currently use the UIGraphicsBeginImageContext function to create bitmaps, you may want to adjust your code to take scale factors into account. The UIGraphicsBeginImageContext function always creates images with a scale factor of 1.0. If the underlying device has a high-resolution screen, an image created with this function might not appear as smooth when rendered. To create an image with a scale factor other than 1.0, use the UIGraphicsBeginImageContextWithOptions instead. The process for using this function is the same as for the UIGraphicsBeginImageContext function:

  1. Call UIGraphicsBeginImageContextWithOptions to create a bitmap context (with the appropriate scale factor) and push it on the graphics stack.
  2. Use UIKit or Core Graphics routines to draw the content of the image.
  3. Call UIGraphicsGetImageFromCurrentImageContext to get the bitmap’s contents.
  4. Call UIGraphicsEndImageContext to pop the context from the stack.

For example, the following code snippet creates a bitmap that is 200 x 200 pixels. (The number of pixels is determined by multiplying the size of the image by the scale factor.)

UIGraphicsBeginImageContextWithOptions(CGSizeMake(100.0,100.0), NO, 2.0);
我感觉,这里写成:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(100.0,100.0), NO, 0.0); 会更合适吧

猜你喜欢

转载自windshg.iteye.com/blog/1671058