Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.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
Objective c 使用imageNamed在设备上失败,但在模拟器中工作_Objective C_Ios_Uiimage - Fatal编程技术网

Objective c 使用imageNamed在设备上失败,但在模拟器中工作

Objective c 使用imageNamed在设备上失败,但在模拟器中工作,objective-c,ios,uiimage,Objective C,Ios,Uiimage,这是我的密码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifie

这是我的密码:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
    NSMutableArray *finalArray = [(NSArray*)filePathsArray mutableCopy];
    NSFileManager *fm = [NSFileManager defaultManager];
    NSArray *files = [fm contentsOfDirectoryAtPath:documentsDirectory error:nil];
    NSString *fileFirst = [files objectAtIndex:0];
    NSString *fullPath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileFirst];
    /* NSUInteger index = [finalArray indexOfObject:@".DS_Store"];
    [finalArray removeObjectAtIndex: index]; */
    //NSLog(@"files array %@", finalArray);
    NSString *title = [[[finalArray objectAtIndex:indexPath.row] lastPathComponent]stringByDeletingPathExtension];
    NSString *image = [[finalArray objectAtIndex:indexPath.row] lastPathComponent];
    NSString *newimage = [[image pathExtension]stringByAppendingPathExtension:@"png"];
    //NSLog(@"%@",newimage);

    if (![UIImage imageNamed:newimage]) {
        newimage = @"notFound.png";
    }

    NSDictionary *fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:nil];
    ////NSLog(@"Here: %@",fileAttribs);
    //long long fileSize = [fileAttribs fileSize];

    NSDate *result = [fileAttribs valueForKey:NSFileCreationDate]; //or NSFileModificationDate
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init]  autorelease];
    [dateFormatter setDateStyle:NSDateFormatterLongStyle];
    NSString *dateString = [dateFormatter stringFromDate:result];   
    [dateFormatter setDateStyle:NSDateFormatterNoStyle];

    cell.textLabel.text = title;
    cell.imageView.image = [UIImage imageNamed:newimage];
    //cell.detailTextLabel.text = [NSString stringWithFormat:@"File extenstion: %@",[[image pathExtension]uppercaseString]];
    cell.detailTextLabel.text = dateString;
    cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}
如上所示,我在users Documents文件夹中获取文件的扩展名,并找到具有该扩展名的图像。这是有效的-我得到了正确的图像PDF.png,例如,当我记录字符串时,但是,如果找不到图像,我希望newimage变量为notFound.png


这在模拟器上有效,但在设备上测试时无效。在设备上,所有cell.imageView.image都显示为notFound.png,即使该单元格记录为PDF.png。有人知道问题出在哪里吗?谢谢。

pdf文件似乎不在您的包中。imageNamed似乎是错误的方法选择

调用imageWithContentsOfFile,但仅针对未找到的图像调用imageNamed

编辑:立即将newimage变量设置为图像

e、 g


Mac OS X使用的文件系统,因此您的模拟器通常配置为不区分大小写。当您创建文件名时,它会保留大小写,但如果您使用错误的大小写进行检索则无关紧要。不过,该设备区分大小写

如果您发现通过imageNamed或任何从本地文件系统读取文件的方式在平台之间查找文件时存在差异,请检查大小写


如果您仍然找不到它,我建议您确保该文件显示在目标的构建阶段,并检查复制包资源部分,基本文件名和扩展名的大小写都正确。我还要确保你进行产品清理并重新构建应用程序,因为有时候Xcode会对复制的文件感到困惑。我还假设您已经退出并重新启动了Xcode,因为当我看到这种罕见的、完全无法解释的行为时,重新启动Xcode或有时甚至我的机器都会修复它。

建议:考虑更好的变量名,使用camelCaseNames并投入时间使代码更易于阅读。不是为了我们,而是为了你自己:更改了标题和标签,希望可以。在使用之前,你是否以某种方式复制了文档目录中的图像?当您将应用程序部署到设备时,只向其发送捆绑资源。+1但是,不区分大小写的不是Mac OSX,而是文件systemHFS+。使用HFS+格式化磁盘时,可以选择文件系统是否区分大小写。除此之外,您还可以装载一个额外的UFS磁盘,例如。这是文件系统。相应地调整了答案。@1789040非常奇怪,因为当我们看到这种行为时,资本化通常是个问题。无论如何,更新后的答案中还有一些其他的想法,这些想法不太可能是罪魁祸首,但要证实也无妨。
id newimagepath = [[image pathExtension]stringByAppendingPathExtension:@"png"];
UIImage *newimage = [UIImage imageWithContentsOfFile:newimagepath];

if(!newimage) {
    newimage = [UIImage imageNamed:@"notFound.ong"]
}