2016-03-20 51 views
2

我能夠調用UIImageWriteToSavedPhotosAlbum和處理完成沒有問題:如何使用靜態函數與完成處理程序調用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 & 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類如果我想使用靜態函數?

回答

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) 

我剛剛進行了上述更改和建議的快速測試;應該彈出一個請求訪問相冊的授權。

+0

謝謝。對不起,關於函數名稱。代碼複製/粘貼時,這是我的錯誤。我嘗試過'someClassName.self',但仍然有應用程序無法找到選擇器方法的錯誤。我終於發現解決方案是'B類'必須繼承'NSObject',那麼錯誤就會消失。 –

+0

但我實際上不明白爲什麼我需要繼承'NSObject'才能工作。如果有人可以提出進一步的意見,這將是有益的。謝謝。 –

相關問題