不使用桥接头访问私有UIKit功能 [英] Access Private UIKit Function Without Using Bridging Header

查看:194
本文介绍了不使用桥接头访问私有UIKit功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

OBJC_EXTERN UIImage *_UICreateScreenUIImage(void) NS_RETURNS_RETAINED;

我可以把它放在一个桥接头中,然后像这样在Swift中访问它:

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

MyApp-Bridging-Header.h

MyApp-Bridging-Header.h

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

MyClass.swift

MyClass.swift

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

有没有办法在不使用桥接头的情况下访问纯Swift中的 _UICreateScreenUIImage

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

最初的想法是在 UIImage 上创建一个扩展,但是扩展期望我在扩展中声明函数的主体: / p>

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"
}

此实现无论如何都是有缺陷的,因为 _UICreateScreenUIImage 不是 UIImage 上的方法。

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

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

Is exposing and accessing this method possible in pure Swift?

人们似乎对我的问题感到困惑我如何截取屏幕截图?那不是我要问的。我问如何在Swift中访问类似 UIImage * _UICreateScreenUIImage(void); 的方法。它可以是任何私有方法,例如 +(UIImage *)_ deviceSpecificImageNamed :( NSString *)name inBundle:(NSBundle *)bundle; + (UIImage *)_ pu_PhotosUIImageNamed:(NSString *)name;

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:

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

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

碰巧, @asmname 实际上只是已在2.3版本中更改为 @_ silgen_name ,所以准备相应调整:

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

据我所知, @_ silgen_name 不提供Objective-C方法的解决方案。为此,有更强大的Objective-C运行时API:

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)

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

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

这篇关于不使用桥接头访问私有UIKit功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆