如何使用静态函数调用带完成处理程序的UIImageWriteToSavedPhotosAlbum()?

时间:2016-03-20 02:03:03

标签: ios swift

我可以调用UIImageWriteToSavedPhotosAlbum并毫无问题地处理完成:

class A {
    func saveNow() {
        UIImageWriteToSavedPhotosAlbum(someImage, self, "saveImage:didFinishSavingWithError:contextInfo:", nil)
    }

    func saveImage(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
        // handle completion
    }
}

但是,如果我想让saveNow()成为静态函数,我无法设置completionTarget&amp; completionSelector正确,以便我可以处理完成:

class B {
    static func saveNow() {
        UIImageWriteToSavedPhotosAlbum(someImage, /* how to set here */)
    }

    static func saveImage completionSelector(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
    }
}

如果我想使用静态函数,如何修改B类?

1 个答案:

答案 0 :(得分:2)

您需要做的是将目标设置为YourClassName.self

即,

UIImageWriteToSavedPhotosAlbum(someImage, YourClassName.self, "saveImage:didFinishSavingWithError:contextInfo:", nil)

顺便说一下,你的函数名称不正确:

//Note the space between saveImage and completionSelector
static func saveImage completionSelector(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) 

我认为应该是:

static func saveImageWithCompletionSelector(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) 

因此,我们可能需要做出相应的改变:

UIImageWriteToSavedPhotosAlbum(someImage, YourClassName.self, "saveImageWithCompletionSelector:didFinishSavingWithError:contextInfo:", nil)

我刚刚对上述变化和建议进行了快速测试;应该有一个弹出窗口请求授权访问相册。