swift中iPhone上的屏幕截图只有白色背景 [英] Screenshotting on Iphone in swift only has a white background

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

问题描述

一些背景:我只是想在按下按钮后使用 xcode 6 beta 7 快速做一个简单的程序来截取 iphone.它是在 SiteKit 和游戏场景中完成的.背景是随机的 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 并在那里设置一个 UIbutton 和一个 IBaction 来截屏,但仍然得到相同的白色背景图像.我尝试了不同的背景图像,结果相同.

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.

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

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! 替换为 view!.window! 以获得唯一的窗口实例.)

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

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

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