您的位置:首页 > 编程语言

iPhone 片断代码:截取屏幕保存图像

2010-04-11 16:27 281 查看

References

http://iphoneincubator.com/blog/tag/uigraphicsbeginimagecontext

Source codes:

UIGraphicsBeginImageContext(imageView.frame.size);

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

NOTES:

1. UIGraphicsBeginImageContext

Creates a bitmap-based graphics context and makes it the current context.
void UIGraphicsBeginImageContext (
CGSize size
);


Parameters
size
The size of the new bitmap context. This represents the size of the image returned by the
UIGraphicsGetImageFromCurrentImageContext
function.

Discussion
You use this function to configure the drawing environment for rendering into a bitmap. This drawing environment uses the premultiplied RGBA format to store the resulting bitmap data. The environment also uses the default coordinate system for UIKit views, where the origin is in the upper-left corner and the positive axes extend down and to the right of the origin. The drawing environment is pushed onto the graphics context stack immediately.

While the context created by this function is the current context, you can call the
UIGraphicsGetImageFromCurrentImageContext
function to retrieve an image object based on the current contents of the context. When you are done modifying the context, you must call the
UIGraphicsEndImageContext
function to clean up the bitmap drawing environment and remove the graphics context from the top of the context stack. You should not use the
UIGraphicsPopContext
function to remove this type of context from the stack.

In most other respects, the graphics context created by this function behaves like any other graphics context. You can change the context by pushing and popping other graphics contexts. You can also get the bitmap context using the
UIGraphicsGetCurrentContext
function.

2. Fill rect

[[UIColor redColor] set];

UIRectFill(CGRectMake(10,10, 200, 100));

3. Draw Text

NSString* greeting = @"Tom, how are you now ";

[greeting drawAtPoint:CGPointMake(205, 0) withFont:[UIFont fontWithName:@"Helvetica" size:36.0]];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: