Guy*_*ood 18 iphone sdk xcode objective-c nsfilemanager
我有一个带有许多图像的应用程序.我想检查捆绑中是否存在图像.如果是,我显示它,如果不是我显示替换图像.
下面的代码是我提出的,但它不起作用.谁能发现什么是错的?
谢谢 !
NSString * photo = [NSString stringWithFormat:@"%d.jpg", UniqueID];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:photo];
if([fileManager fileExistsAtPath:path])
{
[image setImage:[UIImage imageNamed:photo]];
}
else {
NSLog(@"Hello");
[image setImage:[UIImage imageNamed:@"iPhoneHD.png"]];
}
Run Code Online (Sandbox Code Playgroud)
编辑 - 更改了以下Simon的帖子,但仍然无法正常工作.其他声明总是触发.
Rob*_*ill 24
您正在查看App的文档目录.这真的是你期望找到图像的地方吗?如果它们实际上是添加到您的包中的资源,那么您需要这样做:
NSString *path = [[NSBundle mainBundle] pathForResource:uniqueID ofType:@"jpg"];
Run Code Online (Sandbox Code Playgroud)
更简单 - 只需尝试加载指定的图像.如果失败则加载默认值:
UIImage *tempImage = [UIImage imageNamed:photo];
if (!tempImage) {
tempImage = [UIImage imageNamed:@"iPhoneHD.jpg"]
}
[image setImage:tempImage];
Run Code Online (Sandbox Code Playgroud)
Kur*_*kay 17
+(BOOL) fileExistsInProject:(NSString *)fileName
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *fileInResourcesFolder = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
return [fileManager fileExistsAtPath:fileInResourcesFolder];
}
Run Code Online (Sandbox Code Playgroud)
是我的答案.
你有if语句错误的方式,你说如果它不存在,那么使用它,否则使用默认的.....你想...
if([fileManager fileExistsAtPath:path])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13301 次 |
| 最近记录: |