我正在尝试将来自 web 服务的图像存储在文档目录中。对于存储的图像,我想从 TableView 中的文档目录加载它。
可以从 url 访问图像,例如:- http://Address/App/User/Image/Puegeot_E7
从 web 服务中获取是成功的,但是从文档目录中我一直得到空图像。似乎无法找到这里的问题。
- (void)saveImage:(UIImage*)image withName:(NSString *)imageName
{
if (image != nil)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:imageName];
NSLog(@"Saving path %@",path); // Saving path /Users/computername/Library/Developer/CoreSimulator/Devices/7277F3D3-298C-451A-8607-8070A08650C7/data/Containers/Data/Application/D3AF4DD9-A4CD-42C8-A8EB-0F15C271EE61/Documents/CabImage/Puegeot_E7
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
}
- (UIImage *)getImageFromURL:(NSString *)fileURL
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:fileURL];
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:path]];
NSLog(@"data %@",data); //This returns (null)
UIImage *result = [UIImage imageWithData:data];
return result;
}
在 tableView 中,这就是我尝试从文档目录加载的方式。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//default initialisation stuff
NSDictionary *obj = [responseArray objectAtIndex:indexPath.row];
cell.imgVW.image = [UIImage imageNamed:@"default-car.png"];
NSString *imgPath = obj[@"cabimage"];
NSLog(@"imgPath : %@",imgPath); // imgPath : Image/Puegeot_E7
UIImage *imgFromDisk = [self getImageFromURL:imgPath];
NSLog(@"imgFromDisk - %@",imgFromDisk); -> (null)
if (imgFromDisk)
{
cell.imgVW.image = imgFromDisk;
}
else
{
//download
NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@User/%@",BASE_URL,imgPath]];
self.task = [self.session downloadTaskWithURL:imageURL
completionHandler:^(NSURL *location,NSURLResponse *response,NSError *error)
{
NSData *img_data = [NSData dataWithContentsOfURL:imageURL];
if (img_data)
{
UIImage *img = [UIImage imageWithData:img_data];
dispatch_async(dispatch_get_main_queue(), ^{
ListCell *cell = (id)[tableView cellForRowAtIndexPath:indexPath];
cell.imgVW.image = img;
[self saveImage:img withName:imgPath];
});
}
}];
[self.task resume];
}
}
最佳答案
从您在日志中看到的内容来看,问题似乎很明显:
NSLog(@"imgPath : %@",imgPath); // imgPath : Image/Puegeot_E7
该路径不在文档目录中。
为了对此进行更严格的控制,让 save 方法返回保存图像的路径:
- (NSString *)saveImage:(UIImage*)image withName:(NSString *)imageName{
// op code, then:
return path;
}
测试您是否可以读回图像。 (此代码用于测试,不适用于应用程序)。
UIImage *testImage = [UIImage imageNamed:@"some_test_image"];
NSString *path = [self saveImage:testImage withName:@"test_name"];
UIImage *didItWork = [self getImageFromURL:path];
NSLog(@"did it work? %@", didItWork);
回到真正的应用程序...在模型中写入图像返回的路径之后:
UIImage *downloadedImage = // however you get this
NSString * downloadedImageName = // however you get this
NSDictionary *obj = [responseArray objectAtIndex:indexPath.row];
obj[@"cabbage"] = [self saveImage: downloadedImage withName:downloadedImageName];
现在我们知道您的 cellForRowAtIndexPath
方法将看到保存的路径,并且我们从测试中知道从该路径创建图像将起作用。
关于ios - 在 tableView 的文档目录中存储和检索图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37970124/