xcode 将图片保存在 IPHONE 中特定相册的相册中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13601976/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:12:11  来源:igfitidea点击:

Save image in Gallery in specific Albums in IPHONE

iphoneiosxcodeobject

提问by Zohaib

I want to save image in gallery in specific folder. When i click save i am using this code to save image

我想将图像保存在特定文件夹中的画廊中。当我单击保存时,我正在使用此代码来保存图像

- (IBAction)saveImageToGallery:(id)sender {
NSLog(@"SaveImageToGallery");
/*
 -----------------------------------------------------------------------
 THis will hide button and take screen shot then will show buttons again
 -----------------------------------------------------------------------
 */

[AddImagesToCanvasView setHidden:YES];
[ShareOnCanvas setHidden:YES];
[CanvasButtonForRatio setHidden:YES];

for(UIButton *b in self.view.subviews) {
    if([b isKindOfClass:[UIButton class]]) {
        b.hidden = YES;
    }
}

UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

for(UIButton *b in self.view.subviews) {
    if([b isKindOfClass:[UIButton class]]) {
        b.hidden = NO;
    }
}
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

}

}

i want that when i click save than it open photos so i can create new album or click on existing album and save that image in that album. How will i do that. Is this possible?

我希望当我单击保存而不是打开照片时,我可以创建新相册或单击现有相册并将该图像保存在该相册中。我将如何做到这一点。这可能吗?

回答by Midhun MP

You can use the foolowing code to save the image to specific Album.

您可以使用傻瓜代码将图像保存到特定相册。

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library saveImage:image toAlbum:@"Midhun" withCompletionBlock:^(NSError *error) {
        if (error!=nil)
        {
            NSLog(@"Noooo error: %@", [error description]);
        }
    }];

Here image is your UIImage.

这里的图像是你的UIImage.

Make sure to include AssetsLibrary framework and #import <AssetsLibrary/AssetsLibrary.h>

确保包含 AssetsLibrary 框架和 #import <AssetsLibrary/AssetsLibrary.h>

Please refer this tutorial.

请参考本教程

回答by Paras Joshi

use this line with path

将此行与路径一起使用

[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];

use this bellow code if file type is JPEG

如果文件类型为 JPEG,请使用以下代码

[UIImageJPEGRepresentation(image) writeToFile:path atomically:YES];