Ответ 1
Это намного проще, чем вы ожидали:
@asmname("_UICreateScreenUIImage")
func _UICreateScreenUIImage() -> UIImage
// That 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 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()
}