UIView更快的截屏至UIImage(特别是ipad3) imageByRenderingView

UIView截屏会应用于很多的场景,如:
1、具有纸书翻页动作效果的代码广泛用于iOS阅读类应用
2、保存当前场景

代码如下:

#pragma mark -

#pragma mark UIView helpers

@interface UIView(Extended) 

- (UIImage *) imageByRenderingView;

@end

@implementation UIView(Extended)

- (UIImage *) imageByRenderingView {

CGFloat oldAlpha = self.alpha;

self.alpha = 1;

UIGraphicsBeginImageContext(self.bounds.size);

[self.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

self.alpha = oldAlpha;

return resultingImage;

}

@end


但如果处理不好,这里截屏有时候速度会不太理想。

一个建议的解决方案是,让UIView的layer采用CATiledLayer

CATiledLayer会很好的cache你的View.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章