考虑私有C函数
_UICreateScreenUIImage
,它返回当前设备屏幕的UIImage快照:
OBJC_EXTERN UIImage *_UICreateScreenUIImage(void) NS_RETURNS_RETAINED;
我可以把它放在一个桥接头,并在Swift中访问它:
MyApp的桥接,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);在斯威夫特.它可以是任何私有方法,例如(UIImage *)_ deviceSpecificImageNamed:(NSString *)name inBundle:(NSBundle *)bundle;或(UIImage *)_ pu_PhotosUIImage命名:(NSString *)名称;
.
解决方法
这比你想象的要容易得多:
@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方法的@R_301_463@案.为此,还有更强大的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,则不会为您生成.相反,您可以使用返回类型的非托管,并将其包装在函数中,以方便您:
@_silgen_name("_UICreateScreenUIImage") func _UICreateScreenUIImage() -> Unmanaged<UIImage> func UICreateScreenUIImage() -> UIImage { return _UICreateScreenUIImage().takeRetainedValue() }