我想知道我的iPhone应用程序如何利用特定的屏幕截图UIView作为UIImage.
我试过这段代码,但我得到的只是一张空白图片.
UIGraphicsBeginImageContext(CGSizeMake(320,480));
CGContextRef context = UIGraphicsGetCurrentContext();
[myUIView.layer drawInContext:context];
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
myUIView尺寸为320x480,并且有一些子视图.这样做的正确方法是什么?
一些背景:我只是试图用一个简单的程序使用xcode 6 beta 7在swift中按下按钮后截图iphone.它在SpiteKit和游戏场景中完成.背景是随机png图像和"hello world"默认示例文本.我使用以下代码以编程方式在gamescene didMoveToView函数中放置一个可按下按钮(默认的太空船图像是按钮):
button.setScale(0.2)
screen.frame = CGRect(origin: CGPointMake(self.size.width/4, self.size.height/1.5), size: button.size)
screen.setImage(playAgainButton, forState: UIControlState.Normal)
screen.addTarget(self, action: "action:", forControlEvents: UIControlEvents.TouchUpInside)
self.view!.addSubview(screen)
Run Code Online (Sandbox Code Playgroud)
这会在屏幕上设置我的可按下按钮,在该按钮上链接到一个功能,使用下面的代码截取屏幕截图:
func action(sender:UIButton!){
UIGraphicsBeginImageContext(self.view!.bounds.size)
self.view!.layer.renderInContext(UIGraphicsGetCurrentContext())
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)
}
Run Code Online (Sandbox Code Playgroud)
所以这个代码采取截图,但是当我看着照片,只压按钮可以显示与图像的其余部分是白色的.图像如下所示:

下面,我认为屏幕截图应该是这样的,因为这是屏幕在模拟器中的样子(我只是使用了一些随机图像/文本作为背景):

有人可以向我解释为什么屏幕截图程序还没有拍摄背景图片以及如何修复它?
我已经在线查看了,我还没有看到任何问题可以解决我的问题.在线我看到了一些类似的问题并尝试了他们的修复:我导入了quartzCore,coreGraphics和coreImages,但没有修复.此外,我尝试使用ViewController并在那里设置一个带有IBaction的UI按钮进行屏幕截图,但仍然获得相同的白色背景图像.我尝试了不同的背景图像,结果相同.
我对编程很新,所以,任何帮助将不胜感激!先感谢您!