image transfer between iPhone application and server

孤街浪徒 提交于 2020-01-07 01:28:08

问题


There are many kinds of parsing methods like NSXML , JSON etc etc. I am confused with so many methods. So please help me out to choose out of them. Which will be the best parsing method to be followed if images have to be retrieved from and uploaded to a remote server in an iPhone application??


回答1:


ASIHTTPRequest to perform asynchronous requests.
JSON Framework to parse incoming JSON objects into native data objects (NSDictionaries and NSArrays).
UIImage *myDownloadedImage = [UIImage imageWithData:[requestObject responseData]]; to turn downloaded image data into a UIImage.

Here's some sample code from one of my recent projects. I've included ASIHTTPRequest.h and JSON.h.

NSString *projurl = [NSString stringWithFormat:@"%@mobile/project_details/?project=%@", WEBAPPURL, self.projectId];

__block ASIHTTPRequest *r = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:projurl]];
[r setCompletionBlock:^{
    self.projectData = [[r responseString] JSONValue];
    [self.tableView reloadData];
}];
[r startAsynchronous];

You could turn right around in that completion block, pick out image URLs associated with this project, request them, handle the response inside ANOTHER completion block nested inside the first one... The beauty of ASI's new block-oriented result handling is that it can all happen in one place, and you don't have to sweat the details of a formal delegate pattern.

You could put up a "loading" UI element just before the startAsynchronous call, and remove it in the completionBlock, if you wanted.




回答2:


Generally information coming from server will be in xml or JSON format.

NSXML parser parses xml data and JSON parser parses json data. But image will not be in xml or Json format. Image will be chunks of byte which you have to download from the server.

Generally , image url's can be part of xml or json data which will be parsed using appropriate parser . Once you get the url of image, you will use NSUrlConnection or ASIHttpRequest (library) to download the image.




回答3:


  1. Use XML to retrive list of images.(Store it on server or get it from web service)
  2. Use NSXmlParser to parse and get image URL.
  3. Use this for getting image. [NSData dataWithContentsOfURL:<#(NSURL *)url#>] OR Use following.

     NSURL *url = [NSURL URLWithString:[fileUrl
    stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    [theRequest setHTTPMethod:@"POST"];
    
    webData = [[NSMutableData alloc] init];
    
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest    
    delegate:self startImmediately:YES];
    

Or SOAP code

    NSURL *url = [NSURL URLWithString:@"<URL>"];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content- Type"];
    [theRequest addValue: @"<ADD Value Here>" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLConnection theConnection = [[NSURLConnection alloc] initWithRequest:theRequest 
    delegate:self startImmediately:YES];

In above if you are passing URL of file to some web service as request and it returns XML with with file data bound in it then parse it through NSXmlParser and in following method write data in file.

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

write here if you need any further assistance.



来源:https://stackoverflow.com/questions/7283038/image-transfer-between-iphone-application-and-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!