我还没有使用过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;
}