swa*_*ner 77 performance core-graphics graphicscontext ios quartz-core
在我的iPad应用程序中,我想制作UIView占据屏幕重要部分的屏幕截图.不幸的是,子视图非常嵌套,因此需要很长时间才能制作屏幕截图并使页面随后变形.
有没有比"通常"更快的方式?
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
如果可能的话,我想避免缓存或重构我的观点.
3lv*_*vis 111
我找到了一个更好的方法,尽可能使用快照API.
我希望它有所帮助.
class func screenshot() -> UIImage {
var imageSize = CGSize.zero
let orientation = UIApplication.shared.statusBarOrientation
if UIInterfaceOrientationIsPortrait(orientation) {
imageSize = UIScreen.main.bounds.size
} else {
imageSize = CGSize(width: UIScreen.main.bounds.size.height, height: UIScreen.main.bounds.size.width)
}
UIGraphicsBeginImageContextWithOptions(imageSize, false, 0)
for window in UIApplication.shared.windows {
window.drawHierarchy(in: window.bounds, afterScreenUpdates: true)
}
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
Run Code Online (Sandbox Code Playgroud)
Objective-C版本:
+ (UIImage *)screenshot
{
CGSize imageSize = CGSizeZero;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsPortrait(orientation)) {
imageSize = [UIScreen mainScreen].bounds.size;
} else {
imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
}
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
CGContextSaveGState(context);
CGContextTranslateCTM(context, window.center.x, window.center.y);
CGContextConcatCTM(context, window.transform);
CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
if (orientation == UIInterfaceOrientationLandscapeLeft) {
CGContextRotateCTM(context, M_PI_2);
CGContextTranslateCTM(context, 0, -imageSize.width);
} else if (orientation == UIInterfaceOrientationLandscapeRight) {
CGContextRotateCTM(context, -M_PI_2);
CGContextTranslateCTM(context, -imageSize.height, 0);
} else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
CGContextRotateCTM(context, M_PI);
CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
}
if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
[window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
} else {
[window.layer renderInContext:context];
}
CGContextRestoreGState(context);
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Run Code Online (Sandbox Code Playgroud)
Tre*_*kow 18
编辑10月3日2013 更新以支持iOS 7中新的超快速drawViewHierarchyInRect:afterScreenUpdates:方法.
No. CALayer的renderInContext:就我所知,这是唯一的方法.您可以创建这样的UIView类别,以便您自己前进:
UIView的+ Screenshot.h
#import <UIKit/UIKit.h>
@interface UIView (Screenshot)
- (UIImage*)imageRepresentation;
@end
Run Code Online (Sandbox Code Playgroud)
UIView的+ Screenshot.m
#import <QuartzCore/QuartzCore.h>
#import "UIView+Screenshot.h"
@implementation UIView (Screenshot)
- (UIImage*)imageRepresentation {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, self.window.screen.scale);
/* iOS 7 */
if ([self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
else /* iOS 6 */
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* ret = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return ret;
}
@end
Run Code Online (Sandbox Code Playgroud)
通过这种方式,您可以[self.view.window imageRepresentation]
在视图控制器中说出来,并获得应用程序的完整屏幕截图.这可能会排除状态栏.
编辑:
我可以补充一下.如果您有一个透明内容的UIView,并且需要带有底层内容的图像表示,您可以获取容器视图的图像表示并裁剪该图像,只需获取子视图的rect并将其转换为容器即可视图坐标系.
[view convertRect:self.bounds toView:containerView]
Run Code Online (Sandbox Code Playgroud)
裁剪看到这个问题的答案:裁剪UIImage
Kla*_*aas 10
iOS 7引入了一种新方法,允许您将视图层次结构绘制到当前图形上下文中.这可以用来获得UIImage
非常快的速度.
在以下方面实施为类别方法UIView
:
- (UIImage *)pb_takeSnapshot {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Run Code Online (Sandbox Code Playgroud)
它比现有renderInContext:
方法快得多.
SWIFT更新:执行相同的扩展:
extension UIView {
func pb_takeSnapshot() -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.mainScreen().scale);
self.drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)
// old style: self.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
33950 次 |
最近记录: |