Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用NSBundle从指定路径或文档目录获取图像_Ios_Objective C_Ios5_Nsbundle - Fatal编程技术网

Ios 如何使用NSBundle从指定路径或文档目录获取图像

Ios 如何使用NSBundle从指定路径或文档目录获取图像,ios,objective-c,ios5,nsbundle,Ios,Objective C,Ios5,Nsbundle,我做了大量的研究来找出下面代码的工作方式之间的差异。 当我试图从文档目录中获取图像时,可以使用NSBundle通过PATH指定该图像,并在ImageView中显示它 类型1: 此代码工作正常,能够检索图像并显示: NSString *inputPath= @"/Users/abc/Library/Application Support/iPhone Simulator/6.0/Applications/ADD46F96-333A-46BF-8291-FABD1BD7C389/Documents/

我做了大量的研究来找出下面代码的工作方式之间的差异。 当我试图从文档目录中获取图像时,可以使用NSBundle通过PATH指定该图像,并在ImageView中显示它

类型1: 此代码工作正常,能够检索图像并显示:

NSString *inputPath= @"/Users/abc/Library/Application Support/iPhone Simulator/6.0/Applications/ADD46F96-333A-46BF-8291-FABD1BD7C389/Documents/colour.png";
NSString *jjj=[inputPath pathExtension];
NSString *hhhhh=[[inputPath lastPathComponent]stringByDeletingPathExtension];
NSString *bivivik=[inputPath stringByDeletingLastPathComponent];
NSString *imagePATH=[NSBundle pathForResource:hhhhh ofType:jjj inDirectory:bivivik];
theImage=[UIImage imageWithContentsOfFile:imagePATH];
mImageDisplayView.image=theImage;
第2类: 但如果我尝试下面的代码。不获取图像并显示空值

NSString* imagepath = [[NSBundle bundleWithPath:@"/Users/abc/Library/Application Support/iPhone Simulator/6.0/Applications/ADD46F96-333A-46BF-8291-FABD1BD7C389/Documents/colour.png"]bundlePath];
theImage=[UIImage imageWithContentsOfFile:imagepath];
mImageDisplayView.image=theImage;

我的类型2代码有什么问题。当我尝试使用类型2方法时,是否有其他方法可以获取图像。请帮助我

即使上面的代码在一个实例中工作,两个代码段都是错误的,因为它们将无法读取设备上的文件。原因是,您使用的是来自MAC的绝对路径,模拟器可以找到该路径,但该路径在设备上不存在

使用
[NSBundle mainBundle]
从应用程序包读取文件

[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"png"];
要从应用程序的documents目录中读取文件,请使用此代码段

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Myfile.png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath];

编辑

如果您有一个单独的包,其中包含图像文件,那么要从该包中读取,请使用此代码段。假设此捆绑包位于documents目录中

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *bundlePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"MyBundle.bundle"];
NSBundle *myBundle = [NSBundle bundleWithPath:bundlePath];
NSString* imagePath = [myBundle pathForResource:@"MyImage" ofType:@"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath];

希望有帮助

即使上面的代码在一个实例中工作,这两个代码段都是错误的,因为它们将无法读取设备上的文件。原因是,您使用的是来自MAC的绝对路径,模拟器可以找到该路径,但该路径在设备上不存在

使用
[NSBundle mainBundle]
从应用程序包读取文件

[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"png"];
要从应用程序的documents目录中读取文件,请使用此代码段

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Myfile.png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath];

编辑

如果您有一个单独的包,其中包含图像文件,那么要从该包中读取,请使用此代码段。假设此捆绑包位于documents目录中

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *bundlePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"MyBundle.bundle"];
NSBundle *myBundle = [NSBundle bundleWithPath:bundlePath];
NSString* imagePath = [myBundle pathForResource:@"MyImage" ofType:@"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath];

希望有帮助

你的回答被接受了。如果我需要使用NSBundle访问图像,该怎么办。因为我使用的文件路径类似于NSString*imagepath=[[NSBundle bundleWithPath:filePath]bundlePath];“这行得通吗?”lokesh忽略了我之前的评论。上述API不用于获取应用程序包中的文件。它用于获取.bundle文件的路径。您是否从除
mainBundle
之外的单独bundle读取图像文件?是的,我需要使用bundle@lokesh如果我的回答是正确的,您想从存储在文档目录中的捆绑包中读取文件吗?您的回答被接受。如果我需要使用NSBundle访问图像,该怎么办。因为我使用的文件路径类似于NSString*imagepath=[[NSBundle bundleWithPath:filePath]bundlePath];“这行得通吗?”lokesh忽略了我之前的评论。上述API不用于获取应用程序包中的文件。它用于获取.bundle文件的路径。您是否从除
mainBundle
之外的单独bundle读取图像文件?是的,我需要使用bundle@lokesh如果我得到正确的答案,您想从存储在Documents目录中的捆绑包中读取文件吗?