2013-02-19 60 views
-2

我想從服務器中檢索包含10個圖像的文件夾,然後將該文件夾存儲在我的文檔目錄中。我做了一些代碼,但是當我運行它時,我正在獲取圖像url,而不是圖像本身。誰能幫我嗎?如何從服務器檢索一組圖像並將其存儲在iOS中的文檔目錄中?

我的代碼:

-(void)viewWillAppear:(BOOL)animated 
{ 

NSMutableData *receivingData = [[NSMutableData alloc] init]; 
NSURL *url = [NSURL URLWithString:@"http://Someurl.filesCount.php"]; 
NSURLRequest *req = [NSURLRequest requestWithURL:url]; 


    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES]; 

} 

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 

[receivingData appendData:data]; 
} 
-(void) connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
NSError *error = nil; 

{ 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *path = [paths objectAtIndex:0]; 
printf("\n the path is :%s",[path UTF8String]); 


NSString *zipPath = [path stringByAppendingPathComponent:@"filesCount.php"]; 

[receivingData writeToFile:zipPath options:0 error:&error]; 



     NSString *documentsDirectoryPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"filesCount"]; 
     NSLog(@"the path %@",documentsDirectoryPath); 



} 
+0

您正確地下載圖像和保存圖像檢查此鏈接可能是小http://stackoverflow.com/questions/6821517/save-an-image-to-application-documents-folder幫助你-from-uiview-on-ios – Exploring 2013-02-19 06:32:28

回答

1

我在我以前的項目中做過這個功能。您需要傳遞imageView和serverUrl,然後在您的imageView中自動顯示圖像並將圖像保存到臨時目錄,當您想再次獲取相同圖像時,則下一次從磁盤獲取圖像。

+(void)downloadingServerImageFromUrl:(UIImageView*)imgView AndUrl:(NSString*)strUrl{ 

NSFileManager *fileManager =[NSFileManager defaultManager]; 

NSString* theFileName = [NSString stringWithFormat:@"%@.png",[[strUrl lastPathComponent] stringByDeletingPathExtension]]; 
NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"tmp/%@",theFileName]]; 


imgView.backgroundColor = [UIColor darkGrayColor]; 
UIActivityIndicatorView *actView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; 
[imgView addSubview:actView]; 
[actView startAnimating]; 
CGSize boundsSize = imgView.bounds.size; 
CGRect frameToCenter = actView.frame; 
// center horizontally 
if (frameToCenter.size.width < boundsSize.width) 
    frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width)/2; 
else 
    frameToCenter.origin.x = 0; 

// center vertically 
if (frameToCenter.size.height < boundsSize.height) 
    frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height)/2; 
else 
    frameToCenter.origin.y = 0; 

actView.frame = frameToCenter; 


dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
dispatch_async(queue, ^{ 

    NSData *dataFromFile = nil; 
    NSData *dataFromUrl = nil; 

    dataFromFile = [fileManager contentsAtPath:fileName]; 
    if(dataFromFile==nil){ 
     dataFromUrl=[[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:strUrl]] autorelease]; 
    } 

    dispatch_sync(dispatch_get_main_queue(), ^{ 

     if(dataFromFile!=nil){ 
      imgView.image = [UIImage imageWithData:dataFromFile]; 
     }else if(dataFromUrl!=nil){ 
      imgView.image = [UIImage imageWithData:dataFromUrl]; 
      // NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"tmp/%@",theFileName]]; 

      BOOL filecreationSuccess = [fileManager createFileAtPath:fileName contents:dataFromUrl attributes:nil]; 
      if(filecreationSuccess == NO){ 
       NSLog(@"Failed to create the html file"); 
      } 

     }else{ 
      imgView.image = [UIImage imageNamed:@"NO_Image.png"]; 
      imgView.tag = 105; 
     } 
     [actView removeFromSuperview]; 
     [actView release]; 
    }); 
}); 

} 
0

如果你會顯示來自服務器加載UI上的圖像,然後

嘗試SDWebImageView,可以用的UIButton或UIImageView的,很容易和高效使用。

例如,

[yourImageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] 
       placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

how to use it?

好了,現在對多張圖片,你可能需要運行一個循環,如果你對所有圖片的共同基礎URL(在服務器上)如http://yoururl/server/pictures/1.png將被2.png 3.png ... n.png替換,或者您獲得不同url的圖片需要傳遞該url,您可以將其加載到imageview對象中,並稍後將其保存到文檔目錄中(請記住,默認爲您執行此項工作爲SDWebImageView)。你也可以關掉它。

P.S.它將加載一次圖像並存儲到本地(緩存)中,下次您傳遞相同的圖像url時,它不會從服務器加載並直接從本地加載圖像。

+0

我沒有收到你說的@Hemang – 2013-02-19 06:27:50

+0

你有沒有通過任何答案的鏈接? – Hemang 2013-02-19 08:02:24

0

請嘗試使用此代碼。

NSURL* url = [NSURL URLWithString:@"http://imageAddress.com"]; 
NSURLRequest* request = [NSURLRequest requestWithURL:url]; 


[NSURLConnection sendAsynchronousRequest:request 
queue:[NSOperationQueue mainQueue] 
completionHandler:^(NSURLResponse * response, 
    NSData * data, 
    NSError * error) { 
if (!error){ 
     // do whatever you want with directory or store images. 
    NSImage* image = [[NSImage alloc] initWithData:data]; 

} 

}]; 
+0

不,我沒有得到.. @ Vinu1991 – 2013-02-19 06:43:16

+0

用這個替換你的所有代碼。這將爲您提供來自服務器文件夾的所有圖像。您可以將這些文件(圖像)分別存儲到目標目錄中。 – 2013-02-19 06:46:20

+0

我試過但沒有得到你會通過郵件幫助我[email protected]是我的郵件ID .. @ Vinu1991 – 2013-02-19 06:56:43

0

使用此代碼使用URL下載圖像並存儲在文檔目錄中。迭代用於下載和存儲的一組圖像的邏輯。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *imageURL = @"http://sampleUrl"; 
     NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder 
     UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]]; 

     NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"Image1.png"]]; 
     if(image != NULL) 
     { 
      //Store the image in Document 
      NSData *imageData = UIImagePNGRepresentation(image); 
      [imageData writeToFile: imagePath atomically:YES]; 
     } 
相關問題