2011-11-11 4 views
0

こんにちは私は目的のCを使用してURLからローカル電話機ストレージにファイルをダウンロードする方法を示すよいチュートリアルを依頼したい 私は以下の同期を行っていますが、URLからのアシンクダウンロード

NSString* docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
NSString* fileToDownload = @"data1.plist"; 
NSString* hostURLString = @"http://localhost/test"; 
hostURLString = [hostURLString stringByAppendingPathComponent: fileToDownload]; 
NSURL* pListURL = [NSURL URLWithString: hostURLString]; 
NSData* pListData = [NSData dataWithContentsOfURL: pListURL]; 
NSString* filePath = [docsDir stringByAppendingPathComponent: fileToDownload]; 
[pListData writeToFile: filePath atomically: NO]; 


NSString* Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
NSString *DataPath = [Path stringByAppendingPathComponent:@"data1.plist"]; 

任意の提案

+0

可能重複[iPhone:\に対立するものとしてNSURL要求とは非同期にデータをダウンロードする[NSDataのdatawithContents ofURL \]](http://stackoverflow.com/questions/6012612/iphone-downloading-data-asynchronously- with-nsurl-request-as-to-nsdata) –

+0

次のリンクを試してください。非同期で作成してダウンロードするように指示することがあります。 1. http://stackoverflow.com/questions/6820512/async-images-download-in-a-table 2. http://stackoverflow.com/questions/6012612/iphone-downloading-data-asynchronously-with-with- nsurl-request-as-to-nsdata – iOS

答えて

5

以下は、画像とデータを非同期でダウンロードするための私のコードです。あなたの目標に合わせて遊ぶことができます。

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
    dispatch_async(queue, ^{ 
     NSLog(@"Screen %@ - pauseBannerFileImage download starts", self.name);   
     UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:newUrlForImage]]]; 
     NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:newUrlForImage]]; 
     dispatch_sync(dispatch_get_main_queue(), ^{ 
      NSLog(@"!-Screen %@-!pauseBannerFileImage downloaded", self.name); 
      self.pauseBannerFileImage = image; 
     }); 
    }); 
1

URL Loading System Programming GuideUsing NSURLConnectionセクションでは、同期ダウンロードする方法も非同期接続を作成する方法について説明し、そして。

別の方法として、ASIHTTPRequestを使用する方法もあります。これには、同期要求または非同期要求のいずれかを行うこともできます。

+0

ありがとうありがとうございます:) – user1041149