您的位置:首页 > 其它

生成透明的图形

2013-10-31 10:17 204 查看


今天,需要自己画线,并保存线模型,同时虚线部分要透明,采用如下方法:

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

{

self.lineColor = color;

lineWidth = width;

lineType = type;

//opaque:NO 不透明

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


);


[/code]


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:

Call UIGraphicsBeginImageContextWithOptions to create a bitmap context (with the appropriate scale factor) and push it on the graphics stack.

Use UIKit or Core Graphics routines to draw the content of the image.

Call UIGraphicsGetImageFromCurrentImageContext to get the bitmap’s contents.

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); 会更合适吧


分享到:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: