【问题标题】:UIImageWriteToSavedPhotosAlbum not getting completion callbackUIImageWriteToSavedPhotosAlbum 没有获得完成回调
【发布时间】:2018-02-20 14:31:14
【问题描述】:

我正在运行代码UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) 将照片(来自 URL 的图像)保存到相册库。这完美地工作。但是,这是一个盲函数,我想调用一个回调来知道它是否保存成功。

这是我正在尝试的。

            UIImageWriteToSavedPhotosAlbum(image, self, #selector(imageSaved(_:didFinishSavingWithError:contextInfo:)), nil)

            func imageSaved(image: UIImage!, didFinishSavingWithError error: NSError?, contextInfo: AnyObject?) {
                if (error != nil) {
                    print("error")
                    print(error)
                    //Do Something with error associated with image
                } else {
                    // Everything is alright.
                }
            }

我正在尝试在这里使用此代码,但我不断收到Use of unresolved identifier imageSaved(_:didFinishSavingWithError:contextInfo:)

我的问题是我不了解完成选择器,以及 Apple 文档中发生了什么

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;

^ 是什么意思?我该如何处理?

【问题讨论】:

    标签: swift


    【解决方案1】:

    试试这个:

    UIImageWriteToSavedPhotosAlbum(image, self, #selector(imageSaved(image:didFinishSavingWithError:contextInfo:)), nil)
    

    希望对你有帮助!!

    【讨论】:

    • 哦!几乎工作了。然后我尝试,这是我得到的:没有实现methodSignatureForSelector:-前面的麻烦无法识别的选择器-[“AppName.FirebaseHelper methodSignatureForSelector:] 2018-02-20 06:46:43.445648-0800“AppName”[7749:4189179 ] 无法识别的选择器 -AppName.FirebaseHelper methodSignatureForSelector:]
    • 仅供参考,如果这有帮助,我想知道,但我正在测试知道如果用户不允许应用访问他们的相册会发生什么。然后处理一个错误,让他们知道为什么他们没有图像。
    • @RyanB。这是runtime 错误还是compile time
    • 当我点击“下载”并调用函数时出现运行时错误
    • @RyanB。然后它与其他东西有关。虽然如果你想检查权限check this link 并在检查后调用你的函数
    【解决方案2】:

    我有一个类似的问题,我通过使类继承自 NSObject 解决了这个问题。

    【讨论】:

    • 实际上,我尝试了在多个线程中找到的处理程序的每一种组合,但在我尝试从 NSObject 继承方法之前没有任何效果,然后,突然没有错误了 -> +1 我不得不说,我在单例中使用了调用,之前没有继承
    【解决方案3】:

    Apple 文档 中所述:UIImageWriteToSavedPhotosAlbum(::::)

    完成选择器 要调用的 completionTarget 对象的方法选择器。此可选方法应符合以下签名

    - (void)image:(UIImage *)image
        didFinishSavingWithError:(NSError *)error
                     contextInfo:(void *)contextInfo;
    

    所以你在 Swift 中的回调函数必须类似于

    @objc func imageSaved(image:UIImage,didFinishSavingWithError error:Error,contextInfo:UnsafeMutableRawPointer?){
            self.showAlert(title:"Image saved to Photos!", message: "");
        }
    

    【讨论】:

      猜你喜欢
      • 2013-06-21
      • 1970-01-01
      • 2023-03-24
      • 2014-12-15
      • 1970-01-01
      • 2013-02-05
      • 2012-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多