UIImageWriteToSavedPhotosAlbum callback in Swift

Document:

// Adds a photo to the saved photos album.  The optional completionSelector should have the form:
//  - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
func UIImageWriteToSavedPhotosAlbum(image: UIImage!, completionTarget: AnyObject!, completionSelector: Selector, contextInfo: UnsafeMutablePointer<()>)

But what you need to do like this:

@IBAction func onSave(sender: AnyObject) {
    UIImageWriteToSavedPhotosAlbum(self.image.image, self, Selector("image:didFinishSavingWithError:contextInfo:"), nil)
}

func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo: UnsafePointer<()>) {
    dispatch_async(dispatch_get_main_queue(), {
        UIAlertView(title: "Success", message: "This image has been saved to your Camera Roll successfully", delegate: nil, cancelButtonTitle: "Close").show()
    })
}

1 comment

Leave a Reply

Your email address will not be published. Required fields are marked *