在 tableView 中的文档目录中存储和检索图像? [英] Storing and retrieving images from documents directory in tableView?

查看:21
本文介绍了在 tableView 中的文档目录中存储和检索图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将来自 webservice 的图像存储在文档目录中.对于存储的图像,我想从表视图中的文档目录加载它.图像可以从一个 url 访问,例如:- http://Address/App/User/Image/Puegeot_E7

I am trying to store images from webservice in documents directory.For a stored image I want to load it from documents directory in table view. The images are accessible from a url such as :- http://Address/App/User/Image/Puegeot_E7

从 webservice 获取是成功的,但是从文档目录我一直得到空图像.似乎找不到这里出了什么问题.

Fetching from webservice is successful however from the documents directory I keep getting null image.Can't seem to find what's wrong here.

- (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 中,这是我尝试从文档目录加载的方式.

In tableView this is how I am trying to load from documents directory.

- (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];
   }
}

推荐答案

首先,我建议您使用 SDWebImage 库 - 具有缓存支持的异步图像下载器.用法很简单:

To start with, I recommend you use SDWebImage library - an asynchronous image downloader with cache support. Usage is easy enough:

[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                      placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                             completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                ... completion code here ...
                             }];

但是,如果您想按照自己的方式进行.您应该更改 2 行代码:

However, if you wish to do it your own way. You should change thess 2 line of code:

UIImage *imgFromDisk = [self getImageFromURL:imgPath];

进入这个:

UIImage *imgFromDisk = [self getImageFromURL:[imgPath lastPathComponent]];

[self saveImage:img withName:imgPath];

进入这个:

[self saveImage:img withName:[imgPath lastPathComponent]];

这篇关于在 tableView 中的文档目录中存储和检索图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆