gpt4 book ai didi

ios - 如何将图像保存到iOS 9.0中的特定文件夹

转载 作者:行者123 更新时间:2023-12-01 18:47:48 29 4
gpt4 key购买 nike

已经有了使用 ALAssetsLibrary 将图像保存到特定文件夹的答案。但是它已在iOS 9.0中弃用,并给出了警告“改为使用Photos框架中的PHPhotoLibrary”

当我将ALAssetsLibrary更改为PHPhotoLibrary时,此时出现错误。

    [self.library saveImage:image toAlbum:@"myAlbum" withCompletionBlock:^(NSError *error) {
if (error!=nil) {
NSLog(@"Big error: %@", [error description]);
}
}];

*错误
 No visible @interface for 'PHPhotoLibrary' declares the selector 'saveImage:toAlbum:withCompletionBlock:' 

如何使用 PHPhotoLibrary (或任何其他解决方案)将图像保存到特定文件夹中。

先感谢您。

最佳答案

我还没有使用PHPhotoLibrary,但是我知道使用本地文档目录在Obj-C中保存照片的旧方法:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = paths.firstObject;
NSData *imageData = UIImagePNGRepresentation(image);

NSString *imagePath = [NSString stringWithFormat:@"%@/%@",documentsDirectory,imageFolder];

BOOL isDir;
NSFileManager *fileManager= [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:imagePath isDirectory:&isDir])
if(![fileManager createDirectoryAtPath:imagePath withIntermediateDirectories:YES attributes:nil error:NULL])
NSLog(@"Error: folder creation failed %@", documentsDirectory);

[[NSFileManager defaultManager] createFileAtPath:[NSString stringWithFormat:@"%@/%@", imagePath, imageName] contents:nil attributes:nil];
[imageData writeToFile:[NSString stringWithFormat:@"%@/%@", imagePath, imageName] atomically:YES];

要从NSDocumentDirectory中检索该图像:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = paths.firstObject;
NSString *fullPath = [NSString stringWithFormat:@"%@/%@", documentDirectory, imagePath]; // image path is same as above
if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath]) {
UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
return image;
} else {
return nil;
}

关于ios - 如何将图像保存到iOS 9.0中的特定文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33604160/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com