UIGraphicsBeginImageContext和UIGraphicsBeginImageContextWithOptions实现iOS中的截图功能
2016-01-18 10:37
483 查看
UIGraphicsBeginImageContext
创建一个基于位图的上下文(context),并将其设置为当前上下文(context)。方法声明如下:
void UIGraphicsBeginImageContext(CGSize size);
参数size为新创建的位图上下文的大小。它同时是由UIGraphicsGetImageFromCurrentImageContext函数返回的图形大小。
该函数的功能同UIGraphicsBeginImageContextWithOptions的功能相同,相当与UIGraphicsBeginImageContextWithOptions的opaque参数为NO,scale因子为1.0。
UIGraphicsBeginImageContextWithOptions
函数原型为:
void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);
size——同UIGraphicsBeginImageContext
opaque—透明开关,如果图形完全不用透明,设置为YES以优化位图的存储。
scale—–缩放因子
这里需要判断一下UIGraphicsBeginImageContextWithOptions是否为NULL,因为它是iOS 4.0才加入的。
由于JPEG图像是不透明的,所以第二个参数就设为YES。
第三个参数是缩放比例,iPhone 4是2.0,其他是1.0。虽然这里可以用[UIScreen mainScreen].scale来获取,但实际上设为0后,系统就会自动设置正确的比例了。
值得一提的是,图像本身也有缩放比例,普通的图像是1.0(除了UIImage imageNamed:外,大部分API都只能获得这种图像,而且缩放比例是不可更改的),高清图像是2.0。图像的点和屏幕的像素就是依靠2者的缩放比例来计算的,例如普通图像在视网膜显示屏上是1:4,而高清图像在视网膜显示屏上则是1:1。
接下来的drawInRect:把图像画到了当前的image context里,这时就完成了解压缩和重采样的工作了。然后再从image context里获取新的image,这个image的缩放比例也能正确地和设备匹配。
再点下按钮,发现时间已经缩短到12微秒左右了,之后的画图稳定在15微秒左右。
- (UIImage*) takeScreenshot
{
// for the entire screen
// UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
// UIGraphicsBeginImageContext(screenWindow.frame.size);
// [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
// for just the current view (self)
if (iosVersionIsAtLeast(4.0))
UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0.0);
else
UIGraphicsBeginImageContext(self.frame.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
// save to UIImage
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenshot;
}
takeScreenShot returns a UIImage object that can then be written out to a png, attached to an email, or put into a new UIImageView to be animated.
If you are running iOS 4 or greater and your device has a retina screen, use UIGraphicsBeginImageContextWithOptions instead of UIGraphicsBeginImageContext. Setting the scale
(3rd) argument to 0.0 keeps the scale to that of the device’s screen.
创建一个基于位图的上下文(context),并将其设置为当前上下文(context)。方法声明如下:
void UIGraphicsBeginImageContext(CGSize size);
参数size为新创建的位图上下文的大小。它同时是由UIGraphicsGetImageFromCurrentImageContext函数返回的图形大小。
该函数的功能同UIGraphicsBeginImageContextWithOptions的功能相同,相当与UIGraphicsBeginImageContextWithOptions的opaque参数为NO,scale因子为1.0。
UIGraphicsBeginImageContextWithOptions
函数原型为:
void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);
size——同UIGraphicsBeginImageContext
opaque—透明开关,如果图形完全不用透明,设置为YES以优化位图的存储。
scale—–缩放因子
这里需要判断一下UIGraphicsBeginImageContextWithOptions是否为NULL,因为它是iOS 4.0才加入的。
由于JPEG图像是不透明的,所以第二个参数就设为YES。
第三个参数是缩放比例,iPhone 4是2.0,其他是1.0。虽然这里可以用[UIScreen mainScreen].scale来获取,但实际上设为0后,系统就会自动设置正确的比例了。
值得一提的是,图像本身也有缩放比例,普通的图像是1.0(除了UIImage imageNamed:外,大部分API都只能获得这种图像,而且缩放比例是不可更改的),高清图像是2.0。图像的点和屏幕的像素就是依靠2者的缩放比例来计算的,例如普通图像在视网膜显示屏上是1:4,而高清图像在视网膜显示屏上则是1:1。
接下来的drawInRect:把图像画到了当前的image context里,这时就完成了解压缩和重采样的工作了。然后再从image context里获取新的image,这个image的缩放比例也能正确地和设备匹配。
再点下按钮,发现时间已经缩短到12微秒左右了,之后的画图稳定在15微秒左右。
- (UIImage*) takeScreenshot
{
// for the entire screen
// UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
// UIGraphicsBeginImageContext(screenWindow.frame.size);
// [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
// for just the current view (self)
if (iosVersionIsAtLeast(4.0))
UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0.0);
else
UIGraphicsBeginImageContext(self.frame.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
// save to UIImage
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenshot;
}
takeScreenShot returns a UIImage object that can then be written out to a png, attached to an email, or put into a new UIImageView to be animated.
If you are running iOS 4 or greater and your device has a retina screen, use UIGraphicsBeginImageContextWithOptions instead of UIGraphicsBeginImageContext. Setting the scale
(3rd) argument to 0.0 keeps the scale to that of the device’s screen.
相关文章推荐
- UIControl 的基本使用方法和 Target-Action 机制
- UIScreen(屏幕)、UIWindow(画框)、UIView(画布)、didFinishLaunchingWithOptions、UIViewController各个方法的加载顺序
- java_easyui体系之DataGrid(2)[转]
- IOS开发UI-------label
- iOS 在UILabel显示不同的字体和颜色
- UIButton
- mysql_query()与mysql_real_query()
- UIImageView添加阴影和边框
- building workspace 0%问题未解决
- flex URLLoader和URLRequest浅析
- Java学习笔记(38)-----------Query集合
- java_easyui体系之DataGrid(1)[转]
- java_easyui体系之目录 [转]
- bzoj1570: [JSOI2008]Blue Mary的旅行
- This android SDk requires Android developer......
- UIView缩放动画
- Android UI 基础之ViewPager的使用
- UIResponder简要
- 关于UICollectionViewController的header和foot视图
- getJdbcTemplate().query的使用