我想从存储在iPhone中的该图像的已保存文件路径中加载图像。我正在获取路径,但它仍然说文件不存在。这是我的代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",self.imageName]];
BOOL fileExists=[[NSFileManager defaultManager] fileExistsAtPath:imagePath];
NSLog(@"%@",imagePath);
UIImage *fetchImage = [UIImage imageWithContentsOfFile:imagePath];
即使我收到此路径,fileExists也会返回 NO :
/var/mobile/Applications/2ED5C185-8528-48D3-BE0B-28582DC3D294/Documents/IMG_0028.JPG
答案 0 :(得分:7)
我也遇到了这个问题。希望这会让人在路上遇到麻烦。
您不能依赖文档目录来保持从一个构建到下一个构建的一致性。当您重新部署应用程序时,此路径可能会更改:
/var/mobile/Applications/2ED5C185-8528-48D3-BE0B-28582DC3D294/Documents/
我的问题是我将文档路径存储在JSON文件中并尝试在后续版本中检索它。这件作品在每个版本中都会发生变化:
2ED5C185-8528-48D3-BE0B-28582DC3D294
仅存储带扩展名的文件名并动态提取文档路径。然后,您可以继续在文件名上添加路径前缀。
答案 1 :(得分:1)
试试这个对我来说很好用
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *imageName = [[NSString alloc]initWithFormat:@"%@",self.imageName ];
NSString* foofile = [documentsPath stringByAppendingPathComponent:imageName];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];
if (fileExists) {
NSLog(@"alreday Exists");
}
//用于在图像视图上显示图像
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath = [NSString stringWithFormat:@"%@/%@", docDir,self.imageName];
UIImage *img = [UIImage imageWithContentsOfFile:pngFilePath];
答案 2 :(得分:1)
试试这个:
NSString *strNew=[dci valueForKey:@"uThumbPath"];
NSString *strCompare=[NSString stringWithFormat:@"%@.jpg",[self MD5String:strNew]];
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(@"%@",documentsPath);
NSString *strFileName=[NSString stringWithFormat:@"%@/%@",documentsPath,strCompare];
if([[NSFileManager defaultManager] fileExistsAtPath:strFileName])
{
NSLog(@"Downloaded From Localpath");
NSString *inputPath= strFileName;
NSString *extension=[inputPath pathExtension];
NSString *file=[[inputPath lastPathComponent]stringByDeletingPathExtension];
NSString *dir=[inputPath stringByDeletingLastPathComponent];
NSString *imagePATH=[NSBundle pathForResource:file ofType:extension inDirectory:dir];
UIImage* theImage=[UIImage imageWithContentsOfFile:imagePATH];
cell.imgThumb.image=theImage;
}
else{
NSLog(@"Downloaded From Server");
NSURL *URL = [NSURL URLWithString: [dci valueForKey:@"uThumbPath"]];
NSURLRequest* request = [NSURLRequest requestWithURL:URL];
NSLog(@"%@",strNew);
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse * response,
NSData * data1,
NSError * error) {
if (!error){
[self saveLocally:data1 name:strNew];
cell.imgThumb.image = [UIImage imageWithData:data1];
}
}];
}
并使用此
- (NSString *)MD5String:(NSString *)string{
const char *cstr = [string UTF8String];
unsigned char result[16];
CC_MD5(cstr, strlen(cstr), result);
return [NSString stringWithFormat:
@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
}
答案 3 :(得分:0)
UIImage *image =[UIImage imageWithData:[NSData dataWithContentsOfFile:self.aStrImagePath]];
答案 4 :(得分:0)
尝试使用以下代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.JPG",self.imageName]];
BOOL fileExists=[[NSFileManager defaultManager] fileExistsAtPath:imagePath];
NSLog(@"%@",imagePath);
UIImage *fetchImage = [UIImage imageWithContentsOfFile:imagePath];
答案 5 :(得分:-2)
我发生这件事恰好发生在我身上。将XCode添加到我的项目后,XCode没有正确更新我的文件。尝试做一个干净的(产品 - >清洁),看看是否能解决问题。