ios以离线模式存储URL图像(未连接到互联网)

Posted

技术标签:

【中文标题】ios以离线模式存储URL图像(未连接到互联网)【英文标题】:ios store URL images in offline mode(not connected to internet) 【发布时间】:2012-07-20 07:01:57 【问题描述】:

我正在从不同的 url 获取图像数组..图像加载完美..但是如果我需要在离线模式下显示图像..我找不到正确的方法来获取它。这是我的代码

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *arr = [NSArray arrayWithObjects:[NSURL URLWithString: 
                                        @"http://images.wikia.com/marvelmovies/images/5/5e/The-hulk-2003.jpg"],
              [NSURL URLWithString:@"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],
             [NSURL URLWithString:@"http://challenge.roadef.org/2012/img/google_logo.jpg"],
              [NSURL URLWithString:@"http://addyosmani.com/blog/wp-content/uploads/2012/03/Google-doodle-of-Richard-007.jpg"],
              [NSURL URLWithString:@"http://techcitement.com/admin/wp-content/uploads/2012/06/apple-vs-google_2.jpg"],
              [NSURL URLWithString:@"http://www.andymangels.com/images/IronMan_9_wallpaper.jpg"],
              [NSURL URLWithString:@"http://sequelnews.com/wp-content/uploads/2011/11/iphone_5.jpg"],Nil];

NSURL *url=[NSURL URLWithString:arr];

NSData* theData = [NSData dataWithContentsOfURL:url];

int row = 0;
     int column = 0;

     for(int i = 0; i < [(NSArray*) url count]; ++i) 
         UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:(NSURL*)   [(NSArray*)url objectAtIndex:i]]]; 

         UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
         button.frame = CGRectMake(column*100+16, row*90, 80, 80);
         [button setImage:image forState:UIControlStateNormal]; 

         [button addTarget:self 
                    action:@selector(buttonClicked:) 
          forControlEvents:UIControlEventTouchUpInside];
         button.tag = i; 
         [self.view addSubview:button];

         if (column == 2) 
             column = 0;
             row++;
          else 
             column++;
         
           
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:arr];
[theData writeToFile:localFilePath atomically:YES];


请指导...

【问题讨论】:

您可以将这些图像缓存在缓存文件夹中,这样您就不需要一次又一次地下载这些图像,并且当您对这些图像的处理完成时,然后从缓存中删除这些图像。 【参考方案1】:

试试SDWebImage,SDWebImage可以下载图片保存到磁盘缓存,离线使用。

【讨论】:

@Dany 大声喊叫,把最后的逗号去掉就可以了……我会编辑问题 只要问谷歌就会给你这个链接github.com/rs/SDWebImage【参考方案2】:

您应该将图像保存到您的设备中,这样您就可以在未连接到互联网时访问它们。您现在执行此操作的方式总是加载 url。

你可以这样尝试: 为每个图像使用一个 NSData 对象并在其中加载 url 内容。

NSData* theData = [NSData dataWithContentsOfURL:yourURLHere];

现在您可以将此对象保存到您的设备中,并在需要时随时访问它。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"picName.jpg"];
[theData writeToFile:localFilePath atomically:YES];

应该就是这样

【讨论】:

这不是一个好方法(第一部分),因为对于可能需要太多时间的进程来说它不是异步的。

以上是关于ios以离线模式存储URL图像(未连接到互联网)的主要内容,如果未能解决你的问题,请参考以下文章

未连接到互联网时如何显示警报? iOS

IE 11浏览器在尝试访问App Cached离线页面时显示“您未连接到网络”页面

未连接到 xcode 调试模式时,iOS7 后台获取 performFetchWithCompletionHandler 在设备上不起作用

安卓位置选择器

使用 Qtlocation 保存地图

iOS 中的离线通知