简体   繁体   English

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

[英]Stretched UIView background gets cut off during screenshot

So, I am taking a screenshot of a subclassed UIView that I save into the device's photo stream. 所以,我正在截取一个子类UIView的截图,我将其保存到设备的照片流中。

Problem: 问题:

The problem is that I use resizableImageWithCapInsets to add a stretched background to my UIView, but this background gets cut off on the right side and I have no idea why. 问题是我使用resizableImageWithCapInsets向我的UIView添加拉伸背景,但是这个背景在右侧被截断,我不明白为什么。 If someone could help me out it would be highly appreciated. 如果有人可以帮助我,我将非常感激。

这是应用程序中的图像(左)和将其保存到照片流中后的样子(右)

I add the stretched background to my UIView the following way: 我通过以下方式将拉伸的背景添加到我的UIView:

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

Which calls this method: 哪个调用此方法:

- (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;
}

The actual printscreen is done with this method (RINDiagramView is the name of my subclassed UIView, which I am taking a screenshot of). 实际的printcreen是用这个方法完成的(RINDiagramView是我的子类UIView的名字,我正在截取它的截图)。 The rotation is in there because I need the image rotated when I save it, but I commented out that part and that is not what does the background to act weird. 旋转在那里,因为我需要在保存时旋转图像,但我注释掉了那部分而不是背景的行为很奇怪。

- (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;
}

I use Xcode 5.1 and everything is done programmatically (no storyboard and such). 我使用Xcode 5.1,一切都是以编程方式完成的(没有故事板等)。 The base SDK is iOS 7.1. 基础SDK是iOS 7.1。

If you're doing iOS 7+ you can use the new drawViewHierarchyInRect:afterScreenUpdates: and related methods which Apple says are really performant. 如果您正在使用iOS 7+,您可以使用新的drawViewHierarchyInRect:afterScreenUpdates:以及Apple所说的真正drawViewHierarchyInRect:afterScreenUpdates:相关方法。

Even if you're targeting iOS 6 you should give it a try to see if you get the same problem. 即使您的目标是iOS 6,也应该尝试一下,看看是否会遇到同样的问题。

Try using the correct scale? 尝试使用正确的比例?

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

Use a different UIViewContentMode? 使用不同的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

The reason you got a right-side cut image is caused by this line 你有一个右侧切割图像的原因是由这条线引起的

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

You made the image orientation to left, the context will thought the left-side is your top-side.And your size has a minus to the height value, so the result turns to the right-side is cut. 您将图像方向设置为左侧,上下文将认为左侧是您的顶部。并且您的大小具有负值到高度值,因此结果转向右侧被切割。

About the rotation, I added some code into your code.Hopes it is helpful. 关于轮换,我在你的代码中添加了一些代码。希望它有用。

- (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;
}
UIGraphicsBeginImageContext(self.window.bounds.size);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:@"foo.png" atomically:YES];

for retina display, change the first line into this: 对于视网膜显示,将第一行更改为:

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

adjust your size, may you get help.. 调整你的尺寸,你可以得到帮助..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM