迅速在Iphone上截屏只有白色背景 [英] Screenshotting on Iphone in swift only has a white background

查看:325
本文介绍了迅速在Iphone上截屏只有白色背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些背景:我只是想尝试使用xcode 6 beta 7编写一个简单的程序,以便在按下按钮后迅速对iPhone进行屏幕截图.它是在SpiteKit和游戏场景中完成的.背景是随机的png图像和"hello world"默认示例文本.我使用以下代码在游戏场景didMoveToView函数中以编程方式放置了一个可按下按钮(默认的太空飞船图像是该按钮):

Some background: I am just trying to do a simple program using xcode 6 beta 7 in swift to screenshot the iphone after I press a button. It is done in SpiteKit and in the game scene. The background is a random png image and the "hello world" default sample text. I programmatically put a press-able button (the default spaceship image is the button) in gamescene didMoveToView function using the following code:

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)

这将我的可按下按钮设置在屏幕上,该按钮与使用以下代码捕获屏幕截图的功能链接:

This sets my press-able button on the screen where it is linked to a function to take a screenshot using this next code:

func action(sender:UIButton!){
    UIGraphicsBeginImageContext(self.view!.bounds.size)
    self.view!.layer.renderInContext(UIGraphicsGetCurrentContext())
    let screenshot = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)
}

因此该代码确实会截取屏幕截图,但是当我查看照片时,仅显示可按下的按钮,其余图像为白色.图像如下所示:

So this code does take a screenshot, but when I look in photos, only the press-able button is shown and the rest of the image is white. The image is shown below:

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

Below, I think the screen shot should look like this as this is what the screen looks like in the simulator (I just used some random images/text as background):

有人可以向我解释为什么截图程序也不同时拍摄背景图片以及如何修复吗?

Can someone explain to me why the screenshot program is not also taking a picture of the background and how to fix it?

我已经上网了,没有看到任何可以解决我的问题的问题.在网上,我看到了一些类似的问题,并尝试了解决方法:我导入了quartzCore,coreGraphics和coreImages,但没有修复.另外,我尝试使用ViewController并在其中使用IBaction设置UIbutton进行屏幕截图,但仍然获得相同的白色背景图像.我尝试了不同的背景图像,但结果相同.

I've looked online and I have not seen any question that is a solution to my problem. Online I saw some similar problems and tried out their fixes: I imported quartzCore, coreGraphics, and coreImages, but with no fix. Also, I tried using the ViewController and setting a UIbutton on there with a IBaction to screen shot, but still get the same white background image. I've tried different background images with the same result.

我对编程还很陌生,因此,任何帮助将不胜感激!先感谢您!

I am fairly new to programming so, any help would be appreciated! Thank you in advance!

推荐答案

您是否将背景设置为pattern-background-color? =>也许这与renderInContext的效果不符.

Have you set the background as pattern-background-color? => Maybe this doesn't work as expected with renderInContext.

背景图像是以子视图还是其drawRect:方法呈现在self.view中? =>如果没有的话,当然不会被渲染.

Is the background image rendered in self.view, either as a subview or in its drawRect: method? => if not, of course it will not be rendered.

获取屏幕截图的最简单方法是使用功能

The easiest way to get a screenshot is with the function

UIImage *_UICreateScreenUIImage();

您必须先声明它,因为它是未记录的函数.我不确定Apple是否会接受包含对此功能的调用的应用程序,但我认为可以(我知道审阅准则说您不得使用未记录的功能,但在这种情况下,我认为它们不会在意.)该功能可用并在iOS 5-iOS 8中运行(我已经对其进行了测试.不在乎iOS 4.)

you have to declare it first, because its an undocumented function. I'm not sure if Apple will accept an app that contains a call to this function, but I think it will be okay (I know the review guidelines say you must not use undocumented functions, but I think they will not care in this case.) It's available and working in iOS 5 - iOS 8 (i have tested it. don't care about iOS 4.)

另一种可行的方法是渲染整个屏幕,而不只是一个特定的视图:

Another thing that should also work is to render the whole screen instead of just one particular view:

UIGraphicsBeginImageContext(self.view!.window!.bounds.size)
self.view!.window!.layer.renderInContext(UIGraphicsGetCurrentContext())
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)

(请注意,我用view!.window!替换了view!,以获得一个唯一的窗口实例.)

(note that I replaced view! with view!.window! to get the one and only window instance.)

这篇关于迅速在Iphone上截屏只有白色背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆