2012-07-20 69 views
1

我從不同的網址獲取圖像陣列..圖像加載完美..但如果我需要顯示在離線模式下的圖像..我無法找到得到it.Here正確的方式是我的代碼ios商店在離線模式下的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]; 

} 

指導,請...

+0

您可以在緩存文件夾中緩存的公益形象,這樣你就不需要一而再,再而當這些圖片你的工作完成,然後刪除那些下載這些圖片來自緩存的圖像。 – Leena 2012-07-20 10:22:32

回答

1

嘗試SDWebImageSDWebImage可以下載圖片並保存到磁盤高速緩存,您可以在離線使用。

+0

你附加的鏈接不能正常工作......請另行指導.. – Dany 2012-07-20 07:33:49

+0

@Dany大聲哭泣,只是拿出逗號在最後,這是罰款...我會編輯問題 – borrrden 2012-07-20 07:35:16

+0

只要求谷歌會給你這個鏈接https://github.com/rs/SDWebImage/ – Xval 2012-07-20 07:37:09

0

您應該將圖像保存到設備上,以便在未連接到互聯網時訪問它們。您現在的做法是始終加載網址。

你可以這樣嘗試: 爲每個圖像使用一個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]; 

這應該是它

+0

它不能正常工作,我發現線程問題... – Dany 2012-07-20 08:32:42

+0

這不是一個很好的方式來執行它(第一部分),因爲它不是一個可能需要太多時間的進程的異步。 – borrrden 2012-07-22 02:13:14