ios 如何将图片保存到 iPhone 照片库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/178915/
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
How to save picture to iPhone photo library?
提问by jblocksom
What do I need to do to save an image my program has generated (possibly from the camera, possibly not) to the system photo library on the iPhone?
我需要做什么才能将我的程序生成的图像(可能来自相机,也可能不是)保存到 iPhone 上的系统照片库?
回答by Martin Gordon
You can use this function:
您可以使用此功能:
UIImageWriteToSavedPhotosAlbum(UIImage *image,
id completionTarget,
SEL completionSelector,
void *contextInfo);
You only need completionTarget, completionSelectorand contextInfoif you want to be notified when the UIImage
is done saving, otherwise you can pass in nil
.
如果您想在保存完成时收到通知,您只需要completionTarget、completionSelector和contextInfoUIImage
,否则您可以传入nil
.
See the official documentation for UIImageWriteToSavedPhotosAlbum()
.
回答by Denis Fileev
Deprecated in iOS 9.0.
在 iOS 9.0 中已弃用。
There`s much more fast then UIImageWriteToSavedPhotosAlbum way to do it using iOS 4.0+ AssetsLibrary framework
使用 iOS 4.0+ AssetsLibrary 框架比 UIImageWriteToSavedPhotosAlbum 快得多
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
// TODO: error handling
} else {
// TODO: success handling
}
}];
[library release];
回答by Mutawe
The simplest way is:
最简单的方法是:
UIImageWriteToSavedPhotosAlbum(myUIImage, nil, nil, nil);
For Swift
, you can refer to Saving to the iOS photo library using swift
对于Swift
,您可以参考使用 swift 保存到 iOS 照片库
回答by Jeff C.
One thing to remember: If you use a callback, make sure that your selector conforms to the following form:
要记住的一件事:如果您使用回调,请确保您的选择器符合以下形式:
- (void) image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo;
Otherwise, you'll crash with an error such as the following:
否则,您将因如下错误而崩溃:
[NSInvocation setArgument:atIndex:]: index (2) out of bounds [-1, 1]
[NSInvocation setArgument:atIndex:]: index (2) out of bounds [-1, 1]
回答by mrburns05
Just pass the images from an array to it like so
只需像这样将图像从数组传递给它
-(void) saveMePlease {
//Loop through the array here
for (int i=0:i<[arrayOfPhotos count]:i++){
NSString *file = [arrayOfPhotos objectAtIndex:i];
NSString *path = [get the path of the image like you would in DOCS FOLDER or whatever];
NSString *imagePath = [path stringByAppendingString:file];
UIImage *image = [[[UIImage alloc] initWithContentsOfFile:imagePath]autorelease];
//Now it will do this for each photo in the array
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
}
Sorry for typo's kinda just did this on the fly but you get the point
抱歉,打错字有点匆忙,但你明白了
回答by SamChen
When saving an array of photos, don't use a for loop, do the following
保存一组照片时,不要使用for循环,执行以下操作
-(void)saveToAlbum{
[self performSelectorInBackground:@selector(startSavingToAlbum) withObject:nil];
}
-(void)startSavingToAlbum{
currentSavingIndex = 0;
UIImage* img = arrayOfPhoto[currentSavingIndex];//get your image
UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo{ //can also handle error message as well
currentSavingIndex ++;
if (currentSavingIndex >= arrayOfPhoto.count) {
return; //notify the user it's done.
}
else
{
UIImage* img = arrayOfPhoto[currentSavingIndex];
UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
}
回答by King-Wizard
In Swift:
在斯威夫特:
// Save it to the camera roll / saved photo album
// UIImageWriteToSavedPhotosAlbum(self.myUIImageView.image, nil, nil, nil) or
UIImageWriteToSavedPhotosAlbum(self.myUIImageView.image, self, "image:didFinishSavingWithError:contextInfo:", nil)
func image(image: UIImage!, didFinishSavingWithError error: NSError!, contextInfo: AnyObject!) {
if (error != nil) {
// Something wrong happened.
} else {
// Everything is alright.
}
}
回答by iDevAmit
Below function would work. You can copy from here and paste there...
下面的功能会起作用。您可以从这里复制并粘贴到那里...
-(void)savePhotoToAlbum:(UIImage*)imageToSave {
CGImageRef imageRef = imageToSave.CGImage;
NSDictionary *metadata = [NSDictionary new]; // you can add
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:imageRef metadata:metadata completionBlock:^(NSURL *assetURL,NSError *error){
if(error) {
NSLog(@"Image save eror");
}
}];
}
回答by luhuiya
Swift 4
斯威夫特 4
func writeImage(image: UIImage) {
UIImageWriteToSavedPhotosAlbum(image, self, #selector(self.finishWriteImage), nil)
}
@objc private func finishWriteImage(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
if (error != nil) {
// Something wrong happened.
print("error occurred: \(String(describing: error))")
} else {
// Everything is alright.
print("saved success!")
}
}
回答by jarora
In Swift 2.2
在Swift 2.2 中
UIImageWriteToSavedPhotosAlbum(image: UIImage, _ completionTarget: AnyObject?, _ completionSelector: Selector, _ contextInfo: UnsafeMutablePointer<Void>)
If you do not want to be notified when the image is done saving then you may pass nil in the completionTarget, completionSelectorand contextInfoparameters.
如果您不想在图像保存完成时收到通知,那么您可以在completionTarget、completionSelector和contextInfo参数中传递 nil 。
Example:
例子:
UIImageWriteToSavedPhotosAlbum(image, self, #selector(self.imageSaved(_:didFinishSavingWithError:contextInfo:)), nil)
func imageSaved(image: UIImage!, didFinishSavingWithError error: NSError?, contextInfo: AnyObject?) {
if (error != nil) {
// Something wrong happened.
} else {
// Everything is alright.
}
}
The important thing to note here is that your method that observes the image saving should have these 3 parameters else you will run into NSInvocation errors.
这里要注意的重要一点是,您观察图像保存的方法应该具有这 3 个参数,否则您将遇到 NSInvocation 错误。
Hope it helps.
希望能帮助到你。