UIImageWriteToSavedPhotosAlbum选择器语法问题

Agg*_*sor 7 xcode objective-c ios swift

努力让UIImageWriteToSavedPhotosAlbum在swift中运行https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/index.html#//apple_ref/c/func/UIImageWriteToSavedPhotosAlbum

遗憾的是,文件仅在目标C中.

这是我的代码:

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

func saveImageComplete(image:UIImage,err:NSError,context:UnsafePointer<()>)
{
  loadLastPhotoIntoGalleryIcon()
}
Run Code Online (Sandbox Code Playgroud)

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

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: 'app.PhotoEditor<0x14a1e400> does not respond to selector 
saveImageComplete:::'
Run Code Online (Sandbox Code Playgroud)

你能告诉我的语法有什么问题吗?我如何正确地指定这个选择器?根据我的理解,每个:代表方法所期望的1个参数,因为它有3个参数我给它3:'s.

谢谢!

San*_*nju 12

以上都不适合Swift3.试试那些正在努力的人Swift3 Syntax's

行动:

@IBAction func saveToPhotos(_ sender: AnyObject) {

    UIImageWriteToSavedPhotosAlbum(yourImageView.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
Run Code Online (Sandbox Code Playgroud)

目标:

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

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


小智 5

正确的UIImageWriteToSavedPhotosAlbum /选择器代码在这里:

func onYesClicked(action:Int){
    // i'm using optional image here just for example
    if let image = getImage() {
        UIImageWriteToSavedPhotosAlbum(
            image, self,
            Selector("image:didFinishSavingWithError:contextInfo:"),
            nil)
    }
}

func image(
    image: UIImage!,
    didFinishSavingWithError error:NSError!,
    contextInfo:UnsafePointer<Void>)
{
    // process success/failure here
}
Run Code Online (Sandbox Code Playgroud)

这是2016年最新的语法

@IBAction func clickedSaveToUsersCameraRoll()
    {
    print("actually saving yourImage.image to the camera roll, 2016.")
    UIImageWriteToSavedPhotosAlbum(
        yourImage.image!, self,
        #selector(savedOK(_:didFinishSavingWithError:contextInfo:)),
        nil)
    }

func savedOK(
        image:UIImage!,
        didFinishSavingWithError error:NSError!,
        contextInfo:UnsafePointer<Void>)
    {
    print("Wrote photo ok")
    }
Run Code Online (Sandbox Code Playgroud)


Cal*_*leb 4

如果您的方法是 Objective-C 方法,则选择器将类似于“saveImageCompleteImage:err:context:”。您需要记住,参数是 Objective-C 中名称的一部分,因此"saveImageComplete:::"没有指定可以saveImageComplete(image:UIImage,err:NSError,context:UnsafePointer<()>)在 Swift 中调用的方法。