【问题标题】:How to save images into specific folder in iOS 9.0如何在 iOS 9.0 中将图像保存到特定文件夹中
【发布时间】:2015-11-09 07:19:07
【问题描述】:

已经有了答案,使用 ALAssetsLibrary 将图像保存到特定文件夹。但它在 iOS 9.0 中已弃用,并发出警告“请改用照片框架中的 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(或)任何其他解决方案将图像保存到特定文件夹中。

提前谢谢你。

【问题讨论】:

  • 您可能还想参考stackoverflow.com/questions/28348450/… 来讨论专门使用 PHPhotoLibrary 作为 API 的主题。
  • 看到那个链接...保存图片没问题...但我的要求是保存到特定文件夹
  • 在下面查看我的答案,更新以显示如何将二进制图像数据放入特定文件夹。

标签: ios objective-c alassetslibrary ios9.1 phphotolibrary


【解决方案1】:

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

【讨论】:

  • 什么是 imageFolder ?和 imageName ? ...我正在使用 imagePickerController
  • 这些是您创建的字符串。 imagePickerController 会将图像作为 UIImage 从类的委托方法中返回给您。您可以从那里决定如何处理它。
  • 你明白了。我们应该正确提及我们自己的文件夹名称和图像名称
  • 你能告诉我如何从文档目录中获取保存的图像
  • @ChandanReddy 我会尽快更新我上面的答案。
【解决方案2】:

因为没有这样命名的方法。

要将图像保存到 PHPhotoLibrary,您需要创建一个资产。见

Which PHAssetCollection to use for saving an image?

了解详细信息。

【讨论】:

  • 你看到了......但我想保存到单独的文件夹中
猜你喜欢
  • 2013-08-03
  • 1970-01-01
  • 2011-11-08
  • 2018-07-30
  • 2021-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多