Kirby
Kirby

Reputation: 328

Screenshotting on Iphone in swift only has a white background

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:

screenshot

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):

enter image description here

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

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!

Upvotes: 6

Views: 3790

Answers (1)

Michael
Michael

Reputation: 6513

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

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

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)

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

Upvotes: 1

Related Questions