UIImageWriteToSavedPhotosAlbum 选择器语法问题

2024-05-23

努力让 UIImageWriteToSavedPhotosAlbum 快速工作https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/index.html#//apple_ref/c/func/UIImageWriteToSavedPhotosAlbum https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/index.html#//apple_ref/c/func/UIImageWriteToSavedPhotosAlbum

遗憾的是该文档仅在 Objective C 中。

这是我的代码:

func saveImage()
{
  UIImageWriteToSavedPhotosAlbum(uiimage, self, "saveImageComplete:::", nil)
}

func saveImageComplete(image:UIImage,err:NSError,context:UnsafePointer<()>)
{
  loadLastPhotoIntoGalleryIcon()
}

但问题是它会抛出 NSInvalidArgumentException 并带有无法识别的选择器:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: 'app.PhotoEditor<0x14a1e400> does not respond to selector 
saveImageComplete:::'

你能告诉我我的语法有什么问题以及我如何正确指定这个选择器吗?据我了解,每个 : 代表该方法期望的 1 个参数,并且由于它有 3 个参数,所以我给了它 3 个 : 。

Thanks!


以上均不适用于Swift3。对于那些正在苦苦挣扎的人试试这个Swift3 Syntax's

Action:

@IBAction func saveToPhotos(_ sender: AnyObject) {

    UIImageWriteToSavedPhotosAlbum(yourImageView.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}

Target:

func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {

    if error == nil {
        let ac = UIAlertController(title: "Saved!", message: "Image saved to your photos.", preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        present(ac, animated: true, completion: nil)
    } else {
        let ac = UIAlertController(title: "Save error", message: error?.localizedDescription, preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        present(ac, animated: true, completion: nil)
    }
}

请参阅此链接以获取更多说明https://www.hackingwithswift.com/read/13/5/ saving-to-the-ios-photo-library https://www.hackingwithswift.com/read/13/5/saving-to-the-ios-photo-library

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UIImageWriteToSavedPhotosAlbum 选择器语法问题 的相关文章

随机推荐