2013-04-24 60 views
0

我有一个使用URL显示图像的UIImageView,我有一个下载按钮,可以调出一个UIActionSheet,然后使用URL下载文件,我想知道如何编码下载它的一部分使用NSURLConnection。使用NSURLConnection下载

- (IBAction)DownloadClick:(id)sender { 

    UIActionSheet *Action = [[UIActionSheet alloc] 
         initWithTitle:@"Options" 
         delegate:self 
         cancelButtonTitle:@"Cancel" 
         destructiveButtonTitle:nil 
         otherButtonTitles:@"Download", nil]; 

    [Action showInView:self.view]; 
} 

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 

    if (buttonIndex == 0) { //The Download code will go here This is where i need ur help 


      } 

Url被保存在名为“Url”的字符串中。

+0

你尝试过什么下载代码? – Wain 2013-04-24 07:57:58

+0

我尝试了ASIHTTPRequest方法,但从github项目文件 – Vishal 2013-04-24 07:59:53

+0

Google'AFNetworking'或'NSURLConnection示例'中导入类时遇到了很多问题。 – Wain 2013-04-24 08:02:22

回答

1

我不认为你为此尝试过Google。总之,这里是你的代码,不要忘了加NSURLConnectionDataDelegate

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
if (buttonIndex == 0) { //The Download code will go here This is where i need ur help 

    //------------------------------------------------------- 
    NSURL *url = [NSURL URLWithString:@"your_data_url"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url 
              cachePolicy:NSURLRequestUseProtocolCachePolicy 
             timeoutInterval:60.0]; 

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

    if (connection) { 
     NSLog(@"succeed..."); 
    } else { 
     NSLog(@"Failed..."); 
    } 
    //------------------------------------------------------------------- 
    } 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 

} 

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

} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 

} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 

} 
+0

它显示成功,但我必须显示正在下载的文件我该怎么做? – Vishal 2013-04-24 08:36:53

+0

我必须将它保存在手机的图库文件夹中,我该怎么做? – Vishal 2013-04-24 08:42:40

+0

要将图像写入代码保存在didReceiveData中,并且它是否已完成,可以在connectionDidFinishLoading方法中检查。完成下载后它会启动。 – x4h1d 2013-04-24 08:42:57

0

如果您需要同步下载的你可以试试这个代码:

NSURL *imageUrl = [NSURL URLWithString:@"http://site.com/imageName.ext"]; 
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl]; 
UIImage *image = [[UIImage alloc] initWithData:imageData]; 

现在你有,你可以使用图像。

如果你需要一个异步请求,我建议你阅读NSURLConnectionDataDelegate,它可以帮助你处理文件的下载。

1

利用异步请求进行图像下载。它还可以帮助您识别在图像下载过程中是否存在错误。

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){ 
      UIImage* image = [[UIImage alloc] initWithData:data]; 
      // do whatever you want with image 
      } 

}]; 
相关问题