iOS - UIImageWriteToSavedPhotosAlbum

40

我想使用以下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 的意义是什么?可以通过示例解释一下这个参数应该如何使用吗?还是有任何资源可以指导我。

3个回答

111
  • 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 中(即使是最后一个)。请注意保留 HTML 标签。

2
可以加一个Swift版本吗?拜托了 :) - user5306470

5

在现代iOS和Swift中调用UIImageWriteToSavedPhotosAlbum

在现代iOS系统中,使用UIImageWriteToSavedPhotosAlbum有一个额外的要求。您必须在Info.plist文件中包含一个键值NSPhotoLibraryAddUsageDescription("Privacy - Photo Library Additions Usage Description")。这是为了让系统向用户呈现一个对话框,请求写入相机胶卷的权限。

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

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

最后一个参数,上下文(context),通常将是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框架。但是,这是完成工作的简单方法。


如何使用Photos框架完成这个任务? - Awais Fayyaz

-2

基于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
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接