iOS-UIImageWriteToSavedPhotosAlbum [英] iOS - UIImageWriteToSavedPhotosAlbum

查看:237
本文介绍了iOS-UIImageWriteToSavedPhotosAlbum的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wanted to use the following void API to write a captured image to the photo album but I am not very clear about 2 of the parameters

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

根据ADC的解释:

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

completionTarget: optional; the object whose selector should be called after the image has been written to the Camera Roll album.

completionSelector:完成目标对象的方法选择器.此可选方法应符合以下签名:

completionSelector: the method selector of the completionTarget object. This optional method should conform to the following signature:

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

completionTarget在这里的意义是什么?有人可以举例说明如何使用此参数吗?或任何可以指导我完成任务的资源.

What is the significance of completionTarget here? Can someone explain with an example how this parameter should be used? Or any resource which can guide me through it.

推荐答案

  • completionSelector是在图像写入完成后要调用的选择器(方法).
  • completionTarget是要在其上调用此方法的对象.
    • The completionSelector is the selector (method) to call when the writing of the image has finished.
    • The completionTarget is the object on which to call this method.
    • 通常:

      • 您无需在图像写入完成时得到通知(在许多情况下这是没有用的),因此您对两个参数都使用nil
      • 或者您真的想在将图像文件写入相册(或以写入错误结束)时得到通知,并且在这种情况下,通常要实现回调(=在完成时调用的方法)与您从中调用UIImageWriteToSavedPhotosAlbum函数的类相同,因此completionTarget通常为self
      • Either you don't need to be notified when the writing of the image is finished (in many cases that's not useful), so you use nil for both parameters
      • Or you really want to be notified when the image file has been written to the photo album (or ended up with a writing error), and in such case, you generally implement the callback (= the method to call on completion) in the same class that you called the UIImageWriteToSavedPhotosAlbum function from, so the completionTarget will generally be self

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

      As the documentation states, the completionSelector is a selector representing a method with the signature described in the documentation, so it has to have a signature like:

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

      它不必具有确切的名称,但是必须使用相同的签名,即采用3个参数(第一个是UIImage,第二个是NSError,第​​三个是void*类型),不返回任何内容(void).

      It does not have to have this exact name, but it has to use the same signature, namely take 3 parameters (the first being an UIImage, the second an NSError and the third being of void* type) and return nothing (void).

      例如,您可以声明并实现一个可以调用诸如此类的方法:

      You may for example declare and implement a method that you could call anything like this :

      - (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时,您将像这样使用它:

      And when you call UIImageWriteToSavedPhotosAlbum you will use it like this:

      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中添加这些':'(事件是训练中的一个)!

      Note the multiple ':' in the @selector(...) syntax. The colons are part of the method name so don't forget to add these ':' in the @selector (event the trainling one) when you write this line!

      这篇关于iOS-UIImageWriteToSavedPhotosAlbum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆