178*_*040 0 objective-c uiimage ios
这是我的代码:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我在用户Documents文件夹中获取文件的扩展名,并找到具有该扩展名的图像.这是有效的 - 当我NSLog字符串时,我得到了正确的图像(例如PDF.png)但是,如果找不到图像,我希望newimage变量为notFound.png.
这适用于模拟器,但不是在我在设备上测试时.在设备上,所有cell.imageView.image都显示为notFound.png,即使单元格为NSLogs PDF.png(例如).任何人对问题是什么都有任何想法?谢谢.
Mac OS X(以及您的模拟器)使用的文件系统通常配置为不区分大小写(在创建文件名时保留大小写,但如果使用错误的大小写检索则无关紧要).但该设备区分大小写.
如果您发现imageNamed在平台之间通过(或从本地文件系统读取文件的任何内容)查找文件的区别,请检查您的大小写.
如果你还没有找到它,我会建议确保文件显示在目标的"Build Phases"中,并检查"Copy Bundle Resources"部分,并为基本文件名和扩展名指定正确的大小写.我还要确保你做一个"产品" - "清理"并重建应用程序,因为有时Xcode会对已复制的文件感到困惑.我还假设您已退出并重新启动Xcode,因为当我看到这种罕见的,完全无法解释的行为时,重新启动Xcode(有时甚至是我的机器)将会解决它.
| 归档时间: |
|
| 查看次数: |
608 次 |
| 最近记录: |