- (UIImage *)renderImageFromView:(UIView *)the_view { UIGraphicsBeginImageContextWithOptions(the_view.bounds.size, the_view.opaque, 0.0); CGContextRef context=UIGraphicsGetCurrentContext(); [the_view.layer renderInContext:context]; //取得影像 UIImage *the_image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return the_image; }
記得要加入QuartzCore.framework,及
#import <QuartzCore/QuartzCore.h>
一般來說,我們會用這個指令UIGraphicsBeginImageContext(CGSize size);
來指定繪圖區域的大小,但是如果需要指定是否透底色或放大縮小內容,就需要下面著指令UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);
就實用來說,我比較常用 UIGraphicsBeginImageContext(CGSize size)。
一般而言,通常為以 UIGraphicsBeginImageContext 建立繪圖區域,然後開始繪圖,完成之後取得 UIImage (使用UIGraphicsGetImageFromCurrentImageContext),最後以 UIGraphicsEndImageContext 來結束整個繪圖工作。
注:UIGraphicsBeginImageContext(CGSize size)创建一个基于位图的上下文(context),并将其设置为当前上下文。函数功能与UIGraphicsBeginImageContextWithOptions相同,相当于该方法的opaque参数为NO,scale因子为1.0。而UIGraphicsEndImageContext()方法是移除栈顶的基于当前位图的图形上下文
From:http://ikevin.tw/139