【问题标题】:ios passing a void* into UISaveVideoAtPathToSavedPhotosAlbum in Swiftios 在 Swift 中将 void* 传递给 UISaveVideoAtPathToSavedPhotosAlbum
【发布时间】:2014-08-19 17:45:02
【问题描述】:

我是 Swift 新手。我正在尝试在我的班级中使用方法签名:

func UISaveVideoAtPathToSavedPhotosAlbum(_ videoPath: String!,
                                   _ completionTarget: AnyObject!,
                                   _ completionSelector: Selector,
                                   _ contextInfo: CMutableVoidPointer)

最后一个参数 contextInfo 在 obj-c 中是一个 void *。

如果我在 Swift 中传递字典,则会收到 NSDictionary 而不是 CMutableVoidPointer 的子类型错误。对此有任何帮助将不胜感激。我不知道如何在 Swift 中传递 void * 等效参数而不会出现该错误。

【问题讨论】:

    标签: ios objective-c swift void-pointers


    【解决方案1】:

    目标 C - 虚空*

    Swift - UnsafeMutablePointer

    .. 例子

    // 您调用将视频保存到相机胶卷

    UISaveVideoAtPathToSavedPhotosAlbum(yourVideoPath, self, "video:didFinishSavingWithError:contextInfo:", nil)
    

    // 保存视频后的 Completion Handler

        func video(videoPath: String, didFinishSavingWithError error: NSError, contextInfo info: UnsafeMutablePointer<Void>) {
    // your completion code handled here
    
    }
    

    【讨论】:

    • 你得到一个可选的 NSError 对象,所以它应该是 NSError?
    【解决方案2】:
    // Use `var`, not `let`
    var userInfo: NSDictionary = ....
    UISaveVideoAtPathToSavedPhotosAlbum(..., ..., ..., &userInfo)
    

    CMutableVoidPointervoid *
    CConstVoidPointerconst void *

    【讨论】:

    • 这消除了错误,但由于您传入堆栈指针,您的代码不会导致段错误吗?因此,如果声明 userInfo 的框架弹出带有 &userinfo 的回调将是无效的。
    【解决方案3】:

    斯威夫特 4.2

    您可以按照以下几个步骤将 NSDictionary 作为 UnsafeMutablePointer 传递:

    1) 将字典声明为视图控制器的属性

    var userInfo: NSDictionary = [:] 
    

    2) 其次,您需要声明一个在选择器完成异步保存图像/视频时调用的方法:

    @objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
        print(#function)
        let infoDict = Unmanaged<NSDictionary>
                .fromOpaque(contextInfo)
                .takeUnretainedValue()
            as? [String: Any] ?? [:]
        print("infoDict", infoDict )
    }
    

    3) 您需要将 NSDictionary 转换为 UnsafeMutableRawPointer:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        print(#function)
        if let img = info[.originalImage] as? UIImage {
            userInfo = info as NSDictionary
            UIImageWriteToSavedPhotosAlbum(img,
                                           self,
                                           #selector(image),
                                           UnsafeMutableRawPointer(
                                                Unmanaged
                                                .passUnretained(userInfo)
                                                .toOpaque()))
        }
        dismiss(animated: true)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-19
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 2018-11-23
      相关资源
      最近更新 更多