一般:
- 图像写入完成时不需要通知您(在许多情况下这没有用),因此您对两个参数都使用
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 中添加这些':'(当你写这行代码的时候)!