生成透明的图形
2013-10-31 10:17
204 查看
今天,需要自己画线,并保存线模型,同时虚线部分要透明,采用如下方法:
- (UIImage*)lineImage:(UIColor *)color lineWidth:(float)widthlineType:(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
UIGraphicsGetImageFromCurrentImageContextfunction.
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
YESfor this parameter to optimize
the bitmap storage. Specifying
NOmeans 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); 会更合适吧
分享到:
相关文章推荐
- 题目:在opencv中生成透明图形的方法
- C#生成图形验证码
- 将音乐生成波浪图形,JavaScript Html5
- Java Web开发之图形验证码的生成与使用方法
- png图片生成缩略图——背景透明
- java图形验证码生成工具类 web页面校验验证码
- C和OpenGL结合生成钻石图形的解决方案
- [14] 齿轮(Gear Wheel)图形的生成算法
- java图形验证码生成工具类及web页面校验验证码
- 无错版JSP生成图形验证码
- java 中生成图形验证码
- 【PHP】透明水印生成
- Python matplotlib生成图片背景透明
- C# 生成透明Gif图片
- ASP验证码图形生成
- 14_Python随机生成图形
- 【Java】生成图形验证码
- 运用MSChart,生成统计图形
- 支持png透明图片的php生成缩略图类分享
- [12] 扇形体(Fan)图形的生成算法