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.

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