UIImageWriteToSavedPhotosAlbum选择器语法问题

时间:2014-12-09 19:48:14

标签: ios objective-c xcode 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()
}

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

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

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

谢谢!

5 个答案:

答案 0 :(得分:12)

以上@IBAction func saveToPhotos(_ sender: AnyObject) { UIImageWriteToSavedPhotosAlbum(yourImageView.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil) } 无效。试试那些正在努力解决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) } }

的人

动作:

{{1}}

目标:

{{1}}

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

答案 1 :(得分:4)

如果你的方法是Objective-C方法,那么选择器就像&#34; saveImageCompleteImage:err:context:&#34;。您需要记住参数是Objective-C中名称的一部分,因此"saveImageComplete:::"没有指定可以在Swift中调用saveImageComplete(image:UIImage,err:NSError,context:UnsafePointer<()>)的方法。

答案 2 :(得分:4)

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

这是最新语法,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")
    }

答案 3 :(得分:1)

您也可以通过调用单行代码

来完成此操作
   UIImageWriteToSavedPhotosAlbum(imageToSave!,self,nil,nil)

答案 4 :(得分:0)

在Swift 4.1.2中:

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

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

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

}