7

因此,我正在截取我保存到设备照片流中的子类 UIView 的屏幕截图。

问题:

问题是我用来resizableImageWithCapInsets向我的 UIView 添加拉伸背景,但是这个背景在右侧被截断,我不知道为什么。如果有人可以帮助我,将不胜感激。

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

我通过以下方式将拉伸的背景添加到我的 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

4 回答 4

3

如果您正在使用 iOS 7+,您可以使用drawViewHierarchyInRect:afterScreenUpdates:Apple 所说的新的和相关的方法,这些方法非常高效。

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

于 2014-03-31T01:04:57.890 回答
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);

调整你的尺寸,你能得到帮助..

于 2014-04-02T09:18:04.763 回答
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
于 2014-03-27T09:46:36.753 回答
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;
}
于 2014-03-31T12:59:44.467 回答