有没有办法检查我的 iPhone 中是否存在特定图像文件。假设我的图像名称是 IMG_123.JPG。我想搜索此图像,如果它存在,我想使用该文件创建一个 NSInputStream
对象。如果图像不在 NSBundle
中,它可以保存在照片库或其他地方。
请帮帮我。 谢谢
更新
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [pathArray objectAtIndex:0];
NSString* dataPath = [documentsDirectory stringByAppendingPathComponent:@"IMG_2594.JPG"];
更新 2
当用户点击选择器图像时获取图像
copyOfOriginalImage = [UIImage imageWithCGImage:[[imageAsset defaultRepresentation] fullResolutionImage]];
将图片转成NSData
imageData = UIImageJPEGRepresentation(copyOfOriginalImage, 1.0);
然后我把它转换成base64
strEncoded=[imageData base64EncodedStringWithOptions:0];
最佳答案
您的图像可以位于应用程序中的 documentdirectory
或 assets
或 appbundle
。
位于 photolibrary
中的图像,您不能将其称为应用程序数据库的一部分。
因此,您可以检查您的图像是否在 documentdirectory
或任何目录(例如 tempdirectory
或 customdirectory
)中可用,如果您已经做了,
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *filesIndir = [manager contentsOfDirectoryAtPath:path error:nil];
if ([filesIndir containsObject:@"IMG_123.JPG"]) {
NSLog(@"document directory contains : IMG_123.JPG");
}
它将直接在文档目录中检查。如果您在文档目录中创建自定义目录,请提供它的路径。
现在如果你的图像在应用程序包中,那么你可以检查它,
UIImage *image = [UIImage imageNamed:@"IMG_123.JPG"];
if (image) {
NSLog(@"yes image available in app bundle");
}
如果你的图像在 assests
中,那么你可以检查它,
UIImage *image2 = [UIImage imageNamed:@"IMG_123"];
if (image2) {
NSLog(@"yes image available in assets");
}
现在,要检查图像在照片库中是否可用,这是一项有点关键的任务,您应该拥有所有图像的列表,然后您可以检查它是否可用。为此你必须使用 ALAssetsLibrary .
引用This SO answer用于管理这个。
可以引用this so post还有。
关于ios - 如何搜索图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39242568/