检查图像存在于捆绑 – iPhone
发布时间:2020-12-15 01:56:38 所属栏目:百科 来源:网络整理
导读:我有一个应用程序与一些图像。我想检查捆绑中是否存在图像。如果我显示它,如果不显示替换图像。 下面的代码是我想出来的,但是它不起作用。有人会发现有什么问题吗? 谢谢 ! NSString * photo = [NSString stringWithFormat:@"%d.jpg",UniqueID]; NSFileMa
我有一个应用程序与一些图像。我想检查捆绑中是否存在图像。如果我显示它,如果不显示替换图像。
下面的代码是我想出来的,但是它不起作用。有人会发现有什么问题吗? 谢谢 ! 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"]]; } 编辑 – 改变了以下Simon的帖子,但仍然无法正常工作。 Else语句总是触发。 解决方法
您正在查看您的App的Documents目录。这真的是你想要找到图像的地方吗?如果实际上是将资源添加到您的包中,那么您需要执行以下操作:
NSString *path = [[NSBundle mainBundle] pathForResource:uniqueID ofType:@"jpg"]; 更容易 – 只是尝试加载命名的图像。如果失败,则加载默认值: UIImage *tempImage = [UIImage imageNamed:photo]; if (!tempImage) { tempImage = [UIImage imageNamed:@"iPhoneHD.jpg"] } [image setImage:tempImage]; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |