小编典典

在不使用桥接头的情况下访问私有UIKit函数

swift

考虑私有C函数_UICreateScreenUIImage,该函数返回UIImage当前设备屏幕的快照:

OBJC_EXTERN UIImage *_UICreateScreenUIImage(void) NS_RETURNS_RETAINED;

我可以将其放在桥接标头中,然后在Swift中访问它,如下所示:

MyApp-Bridging-Header.h

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

MyClass.swift

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

有没有一种方法可以_UICreateScreenUIImage在不使用桥接头的情况下在纯Swift中访问?

最初的想法是在上创建扩展UIImage,但是该扩展希望我在扩展中声明函数的主体:

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

无论如何,这种实现都是有缺陷的,因为_UICreateScreenUIImage上没有方法UIImage

在纯Swift中是否可以公开和访问此方法?


人们似乎将我的问题与“如何拍摄屏幕截图”混淆了。那不是我要的 我在问如何访问UIImage *_UICreateScreenUIImage(void);Swift中的方法。它可以是任何私有方法,例如+(UIImage *)_deviceSpecificImageNamed:(NSString *)name inBundle:(NSBundle *)bundle;+(UIImage *)_pu_PhotosUIImageNamed:(NSString *)name;


阅读 373

收藏
2020-07-07

共1个答案

小编典典

它比您预期的容易得多:

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

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

碰巧的是,@asmname实际上 只是 在2.3版本中将更改为@_silgen_name,因此准备相应地进行调整:

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

据我所知,@_silgen_name没有提供Objective-C方法的解析。为此,有更强大的Objective-C运行时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)

就处理而言NS_RETURNS_RETAINED,这不会为您生成。相反,您可以使用的返回类型Unmanaged,并将其包装在函数中以方便使用:

@_silgen_name("_UICreateScreenUIImage")
func _UICreateScreenUIImage() -> Unmanaged<UIImage>
func UICreateScreenUIImage() -> UIImage {
    return _UICreateScreenUIImage().takeRetainedValue()
}
2020-07-07