在屏幕截图期间,拉伸的UIView背景被截断

时间:2014-03-21 19:36:57

标签: ios objective-c ipad uiview cgcontextref

所以,我正在截取一个子类UIView的截图,我将其保存到设备的照片流中。

问题:

问题在于我使用resizableImageWithCapInsets为我的UIView添加了拉伸背景,但是这个背景在右侧被截断,我不明白为什么。如果有人能帮助我,我们将非常感激。

This is what the image looks like in the app (left) and after saving it into the photo stream (right)

我通过以下方式将拉伸的背景添加到我的UIView中:

[diagramBase addSubview:[self addTileBackgroundOfSize:diagramBase.frame 
andType:@"ipad_diagram_border.png"]];

调用此方法:

- (UIImageView *) addTileBackgroundOfSize:(CGRect)frame 
andType:(NSString *)type
{
frame.origin.x = 0.0f;
frame.origin.y = 0.0f;

UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:frame];
UIImage *image = [UIImage imageNamed:type];
UIEdgeInsets insets = UIEdgeInsetsMake(10.0f, 10.0f, 10.0f, 10.0f);
UIImage *backgroundImage = [image resizableImageWithCapInsets:insets];
backgroundView.image = backgroundImage;

return backgroundView;
}

实际的打印屏幕是用这种方法完成的(RINDiagramView是我的子类UIView的名字,我正在截取它的截图)。旋转是在那里,因为我需要在保存时旋转图像,但我注释掉了那部分而不是背景的行为很奇怪。

- (UIImage *) createSnapshotOfView:(RINDiagram *) view
{
CGRect rect = [view bounds];
rect.size.height = rect.size.height - 81.0f;
UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *capturedScreen = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage *finalImage = [[UIImage alloc] initWithCGImage: capturedScreen.CGImage
                                            scale: 1.0
                                      orientation: UIImageOrientationLeft];
return finalImage;
}

我使用Xcode 5.1,一切都是以编程方式完成的(没有故事板等)。基础SDK是iOS 7.1。

4 个答案:

答案 0 :(得分:3)

如果你正在使用iOS 7+,你可以使用新的drawViewHierarchyInRect:afterScreenUpdates:以及Apple认为真正有效的相关方法。

即使您定位到iOS 6,也应该尝试一下,看看是否会遇到同样的问题。

答案 1 :(得分:2)

尝试使用正确的比例?

UIImage *finalImage = [[UIImage alloc] initWithCGImage: capturedScreen.CGImage
                                        scale: [[UIScreen mainScreen] scale]
                                  orientation: UIImageOrientationLeft];

使用其他UIViewContentMode?

UIViewContentModeScaleToFill -> check if you can see the edges
UIViewContentModeScaleAspectFit -> check if you can see the edges, even if position is incorrect
UIViewContentModeScaleAspectFill -> check for edge right side

答案 2 :(得分:2)

您获得右侧剪切图像的原因是由此行

引起的
UIImage *finalImage = [[UIImage alloc] initWithCGImage: capturedScreen.CGImage
                                            scale: 1.0
                                      orientation: UIImageOrientationLeft];

你将图像方向调到左边,上下文会认为左边是你的顶边。你的尺寸有一个减去高度的值,所以结果转向右侧被切割。

关于轮换,我在你的代码中添加了一些代码。希望它有用。

- (UIImage *) createSnapshotOfView:(UIView *) view
{
    CGRect rect = [view bounds];
    rect.size.height = rect.size.height - 81.0f;
    UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0f);
    CGContextRef context = UIGraphicsGetCurrentContext();

    view.transform = CGAffineTransformMakeRotation(M_PI_2);
    [view.layer renderInContext:context];

    UIImage *capturedScreen = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    UIImage *finalImage = [[UIImage alloc] initWithCGImage: capturedScreen.CGImage
                                                     scale: 1.0
                                               orientation: UIImageOrientationLeft];

    view.transform = CGAffineTransformMakeRotation(0);
    return finalImage;
}

答案 3 :(得分:2)

UIGraphicsBeginImageContext(self.window.bounds.size);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:@"foo.png" atomically:YES];

用于视网膜显示,将第一行改为:

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale);
else
    UIGraphicsBeginImageContext(self.window.bounds.size);

调整你的体型,你可以得到帮助..