简体   繁体   English

如何从iOS中包含图片的URL获取数据?

[英]How do I fetch Data from a URL that contain an Image in iOS?

I need to fetch JSON data from a URL in iOS6. 我需要从iOS6中的URL提取JSON数据。 How do I get these data into iOS? 如何将这些数据导入iOS? What if there is a URL to an image inside the data, how do I fetch these image too? 如果数据中有图像的URL,该如何获取这些图像呢?

Parse the json and Put the url into an Array or NSString 解析json并将网址放入数组或NSString中

 UIImageView *image1;
 NSString *responseString;//it will contain the url 
 image1.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:responseString]]];
/**
    HTTP CONSTANTS
 **/
#define TIMEOUT_INTERVAL 60
#define CONTENT_TYPE @"Content-Type"
#define URL_ENCODED @"application/x-www-form-urlencoded"
#define GET @"GET"
#define POST @"POST"

-(NSMutableURLRequest*)getNSMutableURLRequestUsingPOSTMethodWithUrl:(NSString *)url postData:(NSString*)_postData
{
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:TIMEOUT_INTERVAL];
    [req setHTTPMethod:POST];
    [req addValue:URL_ENCODED forHTTPHeaderField:CONTENT_TYPE];
    [req setHTTPBody: [_postData dataUsingEncoding:NSUTF8StringEncoding]];
    return req;
}


-(NSMutableURLRequest*)getNSMutableURLRequestUsingGetMethodWithUrl:(NSString*)url
{
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:TIMEOUT_INTERVAL];
    [req setHTTPMethod:GET];
    return req;
}


    NSString *_postData = {<Your postData>}
    NSMutableURLRequest *req = [self getNSMutableURLRequestUsingPOSTMethodWithUrl:LOGIN_URL postData:_postData];
    [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         if (error)// it means server error
         {
             //error while connecting server
         }
         else
         {
             NSError *errorInJsonParsing;
             NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&errorInJsonParsing];
             if(errorInJsonParsing) //error parsing in json
             {
                 //error in json parsing
             }
             else
             {
                 @try
                 {
                    NSLog(@"json==%@",json);
                    // json is basically in key value format which is NSDictionary
                    // if you get the url of image inside this json, then again hit the url to get the image ( u can get the image in NSData which is declared above )
                 }
                 @catch (NSException *exception)
                 {
                     //exception
                 }

             }
         }
     }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM