Access Private UIKit Function Without Using Bridging Header(在不使用桥接头的情况下访问私有 UIKit 函数)
问题描述
考虑私有 C 函数 _UICreateScreenUIImage
,它返回当前设备屏幕的 UIImage
快照:
Consider the private C function _UICreateScreenUIImage
, which returns a UIImage
snapshot of the current device screen:
我可以把它放在一个桥接头中并像这样在 Swift 中访问它:
I can put this in a bridging header and access it in Swift like so:
MyApp-Bridging-Header.h
MyApp-Bridging-Header.h
MyClass.swift
MyClass.swift
有没有一种方法可以在不使用桥接头的情况下在纯 Swift 中访问 _UICreateScreenUIImage
?
Is there a way I can access _UICreateScreenUIImage
in pure Swift without using a bridging header?
最初的想法是在 UIImage
上创建一个扩展,但扩展期望我在扩展中声明函数的主体:
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:
无论如何,这个实现是有缺陷的,因为 _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 *)名称;
.
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
实际上刚刚在 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
不提供 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:
就处理 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:
这篇关于在不使用桥接头的情况下访问私有 UIKit 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!