title: How to get image from json response and display in collection view ios
body: <p>I am new to iOS and I am getting an image url from json.. Now I want to display it on collection view. I am doing the below code. But not able to display image in collection view .Anybody Can help me out for this?<code>enter code here</code></p>
  
  <p>// Collection View Code</p>
  
  <pre><code>- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  {
      return 1;
  }
  
  - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  {
  
      return imageArray.count;
  }
  
  -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  {
  
      UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
  
      if(!cell)
      {
          cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
          UIImageView *myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(50,0, 50, 80)];
          myImageView.tag = 101;
          [cell.contentView addSubview:myImageView];
      }
  
  
      NSString *myImage = [imageArray objectAtIndex:indexPath.row];
  
      NSString *ImageString = [myImage stringByReplacingOccurrencesOfString:@"" withString:@"%20"];
  
      NSLog(@"Image String %@",ImageString);
  
      NSURL *imageURL = [NSURL URLWithString:ImageString];
  
      NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
  
      UIImageView *img = (UIImageView*)[cell.contentView viewWithTag:101];
      img.backgroundColor = [UIColor redColor ];
  
      cell.backgroundView = [UIImage imageWithData:imageData];
  
      return cell;
  
  }
  </code></pre>
  
  <p>// Json Service</p>
  
  <pre><code>NSDictionary *get = @{@"uid":[uidSave valueForKey:@"uid"],@"pro":proNumber};
          NSLog(@"Dictionary Data which is to be get %@",get);
  
          NSData* jsonData = [NSJSONSerialization dataWithJSONObject:get options:kNilOptions error:nil];
          NSString *jsonInputString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
          NSString *post = [[NSString alloc]initWithFormat:@"r=%@",jsonInputString];
  
          NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@",displayCaseUrl]];
  
          NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
          NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
          NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:120.0];
          [request setURL:url];
          [request setHTTPMethod:@"POST"];
          [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
          [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
          [request setHTTPBody:postData];
  
          NSError *error;
          NSURLResponse *response;
          NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&amp;response error:&amp;error];
          if (responseData != nil)
          {
              jsonDict = (NSMutableDictionary *)[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&amp;error];
              NSLog(@"Values =======%@",jsonDict);
  
              NSMutableDictionary *imageDict = [jsonDict valueForKey:@"images"];
              NSLog(@"Image Dictionary :- %@",imageDict);
              imageArray = [imageDict valueForKey:@"image_url"];
              NSLog(@"MY Array Image :- %@",imageArray);
  </code></pre>
  
  <p>// Json Respone from where image is to be get</p>
  
  <pre><code>Image Dictionary :- (
          {
          "image_url" = "http://zapponomics.net/claimservice/parcelimage/1486154806image0.jpg";
          "img_for" = "Front view of parcel";
          "pro_number" = orange;
      }
  )
  2015-08-26 10:09:05.806 ClaimCloud[1997:87342] MY Array String :- (
      "http://zapponomics.net/claimservice/parcelimage/1486154806image0.jpg"
  )
  </code></pre>
  
  <p>//////// USIng Like this but does not work</p>
  
  <pre><code>-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  {
  
      UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
      UIImageView *myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(50,0, 50, 80)];
      myImageView.tag = 101;
      [cell.contentView addSubview:myImageView];
      NSString *myImage = [imageArray objectAtIndex:indexPath.row];
     [self loadImageFromURL:[NSURL URLWithString:[myImage stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] callback:^(UIImage *image) {
          myImageView.image = image;
      }];
      return cell;
  }
  
  - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  {
      return UIEdgeInsetsMake(0, 0, 0,200);
  
  }
  - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  {
      return CGSizeMake(100,80);
  
  }
  - (void) loadImageFromURL: (NSURL*) url callback:(void (^)(UIImage *image))callback {
      dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
      dispatch_async(queue, ^{
          NSData * imageData = [NSData dataWithContentsOfURL:url];
          dispatch_async(dispatch_get_main_queue(), ^{
              UIImage *image = [UIImage imageWithData:imageData];
              callback(image);
          });
      });
  }
  </code></pre>