iOS - UIImageWriteToSavedPhotosAlbum
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7628048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS - UIImageWriteToSavedPhotosAlbum
提问by user682765
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
我想使用以下void API将捕获的图像写入相册,但我对其中2个参数不是很清楚
UIImageWriteToSavedPhotosAlbum (
UIImage *image,
id completionTarget,
SEL completionSelector,
void *contextInfo
);
From the explanation from ADC:
来自ADC的解释:
completionTarget:
optional; the object whose selector should be called after the image has been written to the Camera Roll album.
completionTarget:
可选的; 将图像写入相机胶卷相册后应调用其选择器的对象。
completionSelector:
the method selector of the completionTarget object. This optional method should conform to the following signature:
completionSelector:
completionTarget 对象的方法选择器。此可选方法应符合以下签名:
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo: (void *) contextInfo;
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.
completionTarget
这里有什么意义?有人可以用一个例子来解释这个参数应该如何使用吗?或者任何可以指导我完成它的资源。
回答by AliSoftware
- 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.
- 在
completionSelector
当图像的写入已经完成的是选择器(方法),以调用。 - 的
completionTarget
是在其上调用此方法的对象。
Generally:
一般来说:
- 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 thecompletionTarget
will generally beself
- 要么不需要在图像写入完成时收到通知(在许多情况下这没有用),因此您使用
nil
两个参数 - 或者你真的想在图像文件写入相册时得到通知(或以写入错误结束),在这种情况下,你通常在同一个地方实现回调(=完成时调用的方法)您
UIImageWriteToSavedPhotosAlbum
从中调用函数的类,因此completionTarget
通常是self
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:
正如文档所述,completionSelector
是一个选择器,表示具有文档中描述的签名的方法,因此它必须具有如下签名:
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo: (void *) contextInfo;
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
).
它不必具有这个确切的名称,但必须使用相同的签名,即接受 3 个参数(第一个是 an UIImage
,第二个是 an NSError
,第三个是void*
类型)并且不返回任何内容 ( void
)。
Example
例子
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
}
}
And when you call UIImageWriteToSavedPhotosAlbum
you will use it like this:
当你打电话时,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
Notethe 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!
注意@selector(...)
语法中的多个“:” 。冒号是方法名称的一部分,所以在写这一行时不要忘记在@selector(事件训练)中添加这些':'!
回答by matt
Calling UIImageWriteToSavedPhotosAlbum in modern iOS and Swift
在现代 iOS 和 Swift 中调用 UIImageWriteToSavedPhotosAlbum
In modern iOS, there is an additional requirement for using UIImageWriteToSavedPhotosAlbum. You have to include in your Info.plista key NSPhotoLibraryAddUsageDescription
("Privacy - Photo Library Additions Usage Description"). This is so that the system can present the user with a dialog requesting permission to write into the camera roll.
在现代 iOS 中,使用 UIImageWriteToSavedPhotosAlbum 有一个额外的要求。您必须在Info.plist 中包含一个键NSPhotoLibraryAddUsageDescription
(“隐私 - 照片库添加使用说明”)。这样系统就可以向用户显示一个对话框,请求允许写入相机胶卷。
You can then call UIImageWriteToSavedPhotosAlbum in your code:
然后,您可以在代码中调用 UIImageWriteToSavedPhotosAlbum:
func myFunc() {
let im = UIImage(named:"smiley.jpg")!
UIImageWriteToSavedPhotosAlbum(im, self, #selector(savedImage), nil)
}
The last parameter, the context, will usually be nil
.
最后一个参数,即上下文,通常是nil
.
The idea of the second two parameters, self
and #selector(savedImage)
, is that your savedImage
method in self
will be called back after the image is saved (or not saved). That method should look something like this:
后两个参数self
and的想法#selector(savedImage)
是你的savedImage
方法 inself
会在保存(或不保存)图像后回调。该方法应如下所示:
@objc func savedImage(_ im:UIImage, error:Error?, context:UnsafeMutableRawPointer?) {
if let err = error {
print(err)
return
}
print("success")
}
A typical error would be if the user refused permission in the system dialog. If all goes well, the error will be nil
and you'll know that the write succeeded.
一个典型的错误是如果用户拒绝了系统对话框中的权限。如果一切顺利,就会出现错误nil
,您就会知道写入成功。
In general, UIImageWriteToSavedPhotosAlbum should probably be avoided, in favor of the Photos framework. However, it's a simple way to get the job done.
一般来说,应该避免使用 UIImageWriteToSavedPhotosAlbum,而使用照片框架。但是,这是完成工作的简单方法。
回答by Douglas Pfeifer
SWIFT VERSION based on AliSoftware solution
基于 AliSoftware 解决方案的 SWIFT VERSION
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
}
}