11

Consider the private C function _UICreateScreenUIImage, which returns a UIImage snapshot of the current device screen:

OBJC_EXTERN UIImage *_UICreateScreenUIImage(void) NS_RETURNS_RETAINED;

I can put this in a bridging header and access it in Swift like so:

MyApp-Bridging-Header.h

@import UIKit;
UIImage *_UICreateScreenUIImage(void) NS_RETURNS_RETAINED;

MyClass.swift

let image = _UICreateScreenUIImage()
print(image) // <UIImage: 0x7fc4ba6081c0>, {375, 667}

Is there a way I can access _UICreateScreenUIImage in pure Swift without using a bridging header?

An initial thought was to create an extension on UIImage, but the extension is expecting me to declare the body of the function in the extension:

extension UIImage {
    public func _UICreateScreenUIImage(_: Void) -> UIImage // "Expected '{' in body of function declaration"
}

This implementation is flawed anyways, as _UICreateScreenUIImage isn't a method on UIImage.

Is exposing and accessing this method possible in pure Swift?


People seem to be confusing my question with "How do I take a screenshot?" That's not what I'm asking. I'm asking how do I access methods like UIImage *_UICreateScreenUIImage(void); in Swift. It could be any private method, such as +(UIImage *)_deviceSpecificImageNamed:(NSString *)name inBundle:(NSBundle *)bundle; or +(UIImage *)_pu_PhotosUIImageNamed:(NSString *)name; .

JAL
  • 41,701
  • 23
  • 172
  • 300

1 Answers1

18

It's a lot easier than you would expect:

@asmname("_UICreateScreenUIImage")
func _UICreateScreenUIImage() -> UIImage

// That's it – go ahead and call it:
_UICreateScreenUIImage()

As it happens, @asmname has actually just been changed in the 2.3 builds to @_silgen_name, so be ready to adjust accordingly:

@_silgen_name("_UICreateScreenUIImage")
func _UICreateScreenUIImage() -> UIImage

To my knowledge, @_silgen_name does not provide resolution of Objective-C methods. For this, there is the evenmore powerful Objective-C runtime API:

let invokeImageNamed: (String, NSTimeInterval) -> UIImage? = {
    // The Objective-C selector for the method.
    let selector: Selector = "animatedImageNamed:duration:"
    guard case let method = class_getClassMethod(UIImage.self, selector)
        where method != nil else { fatalError("Failed to look up \(selector)") }

    // Recreation of the method's implementation function.
    typealias Prototype = @convention(c) (AnyClass, Selector, NSString, NSTimeInterval) -> UIImage?
    let opaqueIMP = method_getImplementation(method)
    let function = unsafeBitCast(opaqueIMP, Prototype.self)

    // Capture the implemenation data in a closure that can be invoked at any time.
    return { name, interval in function(UIImage.self, selector, name, interval) }
}()

extension UIImage {
    // Convenience method for calling the closure from the class.
    class func imageNamed(name: String, interval: NSTimeInterval) -> UIImage? {
        return invokeImageNamed(name, interval)
    }
}

UIImage.imageNamed("test", interval: 0)

As far as handling NS_RETURNS_RETAINED, this won't be generated for you. Instead, you can use a return type of Unmanaged, and wrap that in a function to your convenience:

@_silgen_name("_UICreateScreenUIImage")
func _UICreateScreenUIImage() -> Unmanaged<UIImage>
func UICreateScreenUIImage() -> UIImage {
    return _UICreateScreenUIImage().takeRetainedValue()
}
Sam R.
  • 674
  • 4
  • 7
  • 1
    This works! Amazing! Where are `asmname` and `_silgen_name` documented? Is this the equivalent of `extern C` in Swift? – JAL Feb 05 '16 at 19:27
  • 2
    To my knowledge is not documented (outside of LLVM's internals I'm sure). The term `asmname` would indicate that it is indeed equivalent to `extern` in that it simply instructs the assembler to seek out the symbol elsewhere. `_silgen_name` would more specifically mean that it produces `builtin ""()` declarations during SIL generation (which in turn are resolved by the assembler similar to `extern`). – Sam R. Feb 05 '16 at 19:50
  • 2
    Thank you very much! I expect to mark this as accepted and award the bounty to you. Another followup question: What if I wanted to access a private class method on `UIImage` itself, such as: `+(UIImage *)_pu_PhotosUIImageNamed:(NSString *)name;`? Would that follow the same pattern? – JAL Feb 05 '16 at 19:53
  • 1
    Additionally, how to I mark the function as `NS_RETURNS_RETAINED` so I don't have to manage my memory manually? – JAL Feb 05 '16 at 21:05
  • 1
    Not quite the same, unfortunately. I've edited my answer to cover that and the `NS_RETURNS_RETAINED` question. – Sam R. Feb 05 '16 at 21:52
  • 1
    Thanks for following up, appreciate your incredible answer! – JAL Feb 06 '16 at 00:19