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;
.
It's a lot easier than you would expect:
As it happens,
@asmname
has actually just been changed in the 2.3 builds to@_silgen_name
, so be ready to adjust accordingly:To my knowledge,
@_silgen_name
does not provide resolution of Objective-C methods. For this, there is the evenmore powerful Objective-C runtime API:As far as handling
NS_RETURNS_RETAINED
, this won't be generated for you. Instead, you can use a return type ofUnmanaged
, and wrap that in a function to your convenience: