UIGraphicsBeginImageContext和UIGraphicsBeginImageContextWithOptions实现iOS中的截图功能

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.


猜你喜欢

转载自blog.csdn.net/baidu_33425843/article/details/51050612
今日推荐