0

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

- (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);

}

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

4

2 回答 2

6

您可以使用愚蠢的代码将图像保存到特定的相册。

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

这里的图像是你的UIImage

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

请参考本教程

于 2012-11-28T09:54:48.117 回答
0

将此行与路径一起使用

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

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

[UIImageJPEGRepresentation(image) writeToFile:path atomically:YES];
于 2012-11-28T09:44:30.130 回答