xcode UIImageWriteToSavedPhotosAlbum 选择器语法问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27387525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:20:24  来源:igfitidea点击:

UIImageWriteToSavedPhotosAlbum Selector Syntax Issue

iosobjective-cxcodeswift

提问by Aggressor

Trying hard to get UIImageWriteToSavedPhotosAlbum to work in swift https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/index.html#//apple_ref/c/func/UIImageWriteToSavedPhotosAlbum

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

The documentation is sadly ONLY in objective C.

遗憾的是,文档仅在目标 C 中。

Here is my code:

这是我的代码:

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

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

But the problem is that it throws the NSInvalidArgumentException with an unrecognized selector:

但问题是它抛出了 NSInvalidArgumentException 和一个无法识别的选择器:

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

Can you advise what is wrong with my syntax and how I properly specific this selector? From what I understand, each : represents 1 argument the method expects and since it has 3 parameters I gave it 3 :'s.

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

Thanks!

谢谢!

采纳答案by Caleb

If your method were an Objective-C method, the selector would be something like "saveImageCompleteImage:err:context:". You need to remember that the parameters are part of the name in Objective-C, so "saveImageComplete:::"doesn't specify a method that could be called saveImageComplete(image:UIImage,err:NSError,context:UnsafePointer<()>)in Swift.

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

回答by Sanju

Above none work for Swift3. Try this for those who are struggling with Swift3 Syntax's

以上没有为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)
    }
}

Refer this link for more clarification 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

回答by Anthony Akentiev

The correct UIImageWriteToSavedPhotosAlbum/selector code is here:

正确的 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
}

Here's the most up-to-date syntax, 2016

这是 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")
    }

回答by 0xa6a

In Swift 4.x and 5.0:

在 Swift 4.x 和 5.0 中:

UIImageWriteToSavedPhotosAlbum(scrollView.screenshot()!, self, #selector(saveImageComplete(image:err:context:)), nil)

Don't forget the @objcdecorator in it's declaration:

不要忘记@objc声明中的装饰器:

@objc private func saveImageComplete(image:UIImage, err:NSError, context:UnsafeMutableRawPointer?) {

}

回答by Anny

You can do this by calling single line of code also

您也可以通过调用单行代码来做到这一点

   UIImageWriteToSavedPhotosAlbum(imageToSave!,self,nil,nil)