【问题标题】:iOS - UIImageWriteToSavedPhotosAlbumiOS - UIImageWriteToSavedPhotosAlbum
【发布时间】:2011-10-02 17:37:36
【问题描述】:

我想使用下面的void API将捕获的图像写入相册但我对其中2个参数不是很清楚

UIImageWriteToSavedPhotosAlbum (
   UIImage  *image,
   id       completionTarget,
   SEL      completionSelector,
   void     *contextInfo
);

来自ADC的解释:

completionTarget: 可选;在将图像写入相机胶卷相册后应调用其选择器的对象。

completionSelector:completionTarget 对象的方法选择器。此可选方法应符合以下签名:

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo: (void *) contextInfo;

completionTarget 这里的意义是什么?有人可以用一个例子来解释这个参数应该如何使用吗?或者任何可以指导我完成它的资源。

【问题讨论】:

    标签: ios


    【解决方案1】:
    • completionSelector 是图像写入完成时调用的选择器(方法)。
    • completionTarget 是调用此方法的对象。

    一般:

    • 图像写入完成时不需要通知您(在许多情况下这没有用),因此您对两个参数都使用nil
    • 或者你真的想在图片文件写入相册(或者写入错误结束)时得到通知,这种情况下,你通常实现回调(=完成时调用的方法)在您调用 UIImageWriteToSavedPhotosAlbum 函数的同一类中,因此 completionTarget 通常为 self

    正如文档所述,completionSelector 是一个选择器,表示具有文档中描述的签名的方法,因此它必须具有如下签名:

    - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo: (void *) contextInfo;
    

    它不必有这个确切的名称,但它必须使用相同的签名,即采用3个参数(第一个是UIImage,第二个是NSError,第三个是void*类型) 并且什么也不返回 (void)。


    示例

    例如,您可以声明并实现一个方法,您可以像这样调用任何方法:

    - (void)thisImage:(UIImage *)image hasBeenSavedInPhotoAlbumWithError:(NSError *)error usingContextInfo:(void*)ctxInfo {
        if (error) {
            // Do anything needed to handle the error or display it to the user
        } else {
            // .... do anything you want here to handle
            // .... when the image has been saved in the photo album
        }
    }
    

    当你调用UIImageWriteToSavedPhotosAlbum 时,你会这样使用它:

    UIImageWriteToSavedPhotosAlbum(theImage,
       self, // send the message to 'self' when calling the callback
       @selector(thisImage:hasBeenSavedInPhotoAlbumWithError:usingContextInfo:), // the selector to tell the method to call on completion
       NULL); // you generally won't need a contextInfo here
    

    注意@selector(...) 语法中的多个“:”。冒号是方法名称的一部分,所以不要忘记在@selector 中添加这些':'(当你写这行代码时)(如果是training 的话)!

    【讨论】:

    • 您可以添加一个 Swift 版本吗?请问好看吗? :)
    【解决方案2】:

    在现代 iOS 和 Swift 中调用 UIImageWriteToSavedPhotosAlbum

    在现代 iOS 中,使用 UIImageWriteToSavedPhotosAlbum 有一个额外的要求。您必须在 Info.plist 中包含一个密钥 NSPhotoLibraryAddUsageDescription(“隐私 - 照片库添加使用说明”)。这样系统就可以向用户显示一个对话框,请求允许写入相机胶卷。

    然后您可以在代码中调用 UIImageWriteToSavedPhotosAlbum:

    func myFunc() {
        let im = UIImage(named:"smiley.jpg")!
        UIImageWriteToSavedPhotosAlbum(im, self, #selector(savedImage), nil)
    }
    

    最后一个参数,上下文,通常是nil。

    后两个参数self和#selector(savedImage)的思想是你在self中的savedImage方法会在图片保存(或不保存)后被回调。该方法应如下所示:

    @objc func savedImage(_ im:UIImage, error:Error?, context:UnsafeMutableRawPointer?) {
        if let err = error {
            print(err)
            return
        }
        print("success")
    }
    


    如果用户在系统对话框中拒绝了权限,则会出现典型错误。如果一切顺利,错误将为nil,您将知道写入成功。

    一般来说,应该避免使用 UIImageWriteToSavedPhotosAlbum,而使用 Photos 框架。但是,这是完成工作的简单方法。

    【讨论】:

    • 如何使用照片框架做到这一点?
    【解决方案3】:

    基于 AliSoftware 解决方案的 SWIFT 版本

    UIImageWriteToSavedPhotosAlbum(
        yourImage,
        self, // send the message to 'self' when calling the callback
        #selector(image(path:didFinishSavingWithError:contextInfo:)), // the selector to tell the method to call on completion
        nil // you generally won't need a contextInfo here
    )
    
    @objc private func image(path: String, didFinishSavingWithError error: NSError?, contextInfo: UnsafeMutableRawPointer?) {
        if ((error) != nil) {
            // Do anything needed to handle the error or display it to the user
        } else {
            // .... do anything you want here to handle
            // .... when the image has been saved in the photo album
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-09
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多