Screenshot showing up blank - Swift
Asked Answered
B

3

6

Hi I am making a game and I have added a share button to the game. I want the user to be able to share a message, URL, and screenshot along side each other in one message.The sharing aspect of it is working fine and everything is showing up except that the screenshot itself is showing up blank. This is the code I am using to take a screenshot:

   let layer = UIApplication.sharedApplication().keyWindow!.layer
    let scale = UIScreen.mainScreen().scale
    UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);

    layer.renderInContext(UIGraphicsGetCurrentContext())
    let screenshot = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)

    println("screenshot")

Please help me resolve this issue and please be sure to do so in the Swift language. Also I am using SpriteKit Technology if that makes a difference. I am new to coding so please be very clear. Thank you very much!

Bloodfin answered 25/4, 2015 at 1:31 Comment(0)
B
14

update: Xcode 8.2.1 • Swift 3.0.2

You need to add the import statement and this extension to your game scene:

import UIKit 

extension UIView {
    var snapshot: UIImage? {
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0)
        defer { UIGraphicsEndImageContext() }
        drawHierarchy(in: bounds, afterScreenUpdates: true)
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}

let myImage = view?.snapshot
Beecham answered 25/4, 2015 at 2:26 Comment(1)
could you please take a look at my question #43246982. I would really appreciate any helpOzan
C
6

In WWDC 2015, What's new in UIVisualEffectView, a solution is "glossed over" for capturing a screenshot of a UIVisualEffectView containing a vibrancy label and a blur. The problem that often arises is that Apple's screenshot API tends not to capture the UIVisualEffect, resulting in a screenshot of the view without the visual effect. The solution according to WWDC 2015 involves capturing the snapshot at the window/screen; unfortunately, sample code was not shown. Following is my own implementation:

//create snapshot of the blurView
let window = UIApplication.sharedApplication().delegate!.window!!
//capture the entire window into an image
UIGraphicsBeginImageContextWithOptions(window.bounds.size, false, UIScreen.mainScreen().scale)
window.drawViewHierarchyInRect(window.bounds, afterScreenUpdates: true)
let windowImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//now position the image x/y away from the top-left corner to get the portion we want
UIGraphicsBeginImageContext(blurView.frame.size)
windowImage.drawAtPoint(CGPoint(x: -blurView.frame.origin.x, y: -blurView.frame.origin.y))
let croppedImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
//embed image in an imageView, supports transforms.
let resultImageView = UIImageView(image: croppedImage)

Note: This snapshot code will only work once ViewDidAppear has been called.

Chromyl answered 10/11, 2015 at 15:20 Comment(0)
T
0

In my case I was taking screen shot while scanning the passport with scandit SDK and in result that was a blank (black) image, Then I took image from scandit image buffer and used that image.

There might be some camera SDK you will be using in your app which is preventing to take screen shot and in result return blank (black) image.

Takahashi answered 29/5, 2023 at 9:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.