如何在 iOS 9.0 中将图像保存到特定文件夹
How to save images into specific folder in iOS 9.0
已有答案,使用ALAssetsLibrary将图像保存到特定文件夹。但它在 iOS 9.0 中被弃用并发出警告 "Use PHPhotoLibrary from the Photos framework instead".
当我将 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,您需要创建一个资产。参见
Which PHAssetCollection to use for saving an image?
了解详细信息。
我还没有使用过 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;
}
已有答案,使用ALAssetsLibrary将图像保存到特定文件夹。但它在 iOS 9.0 中被弃用并发出警告 "Use PHPhotoLibrary from the Photos framework instead".
当我将 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,您需要创建一个资产。参见
Which PHAssetCollection to use for saving an image?
了解详细信息。
我还没有使用过 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;
}