-1

How to get image by JSON, i trying to many times but could not get images. I want to get image and set on image view and auto slide images. how it possible please help me. I am very tired:(

JSON View

{

"status": [

{
  "id": "80",
  "slider_image_path": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/uploads\/1464670561-GolfOuting_080715.jpg"
},
{
  "id": "81",
  "slider_image_path": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/uploads\/1464708964-facebookGoshenRepair1.jpg"
}
          ]

}

ViewController

 #import "ViewController.h"
@interface ViewController ()
{
  NSArray*namearry;
}

@end

@implementation ViewController
@synthesize imageView;

- (void)viewDidLoad {
[super viewDidLoad];


NSURLRequest *req=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://edutimeapp.com/toshow/chamber-of-commerc/ws/fetch_slider.php"]];
response =[[NSMutableData alloc]init];
[NSURLConnection connectionWithRequest:req delegate:self];


imageView.animationImages = namearry;
imageView.animationDuration = 10;
imageView.animationRepeatCount = 0;
[imageView startAnimating];
 }

 - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[response appendData:data];
NSLog(@"error receving data %@",response);
}

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

 }
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error;

NSLog(@"Error in receiving data %@",error);
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves  error:&error];
NSLog(@"response data %@",json);

NSArray *results = [json objectForKey:@"status"];
namearry = [results valueForKey:@"slider_image_path"];

}
@end
Ankur Kumawat
  • 91
  • 2
  • 12

1 Answers1

0

From this point in your code:

NSArray *results = [json objectForKey:@"status"];
//now go through the array getting each image
for(NSDictionary *item in results) {
    NSString *imageURL = item[@"slider_image_path"];

    //short cut method to grab the image (credit below)
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];

    //we're on a background thread, so switch to main thread to update the UI
    dispatch_async(dispatch_get_main_queue(), ^{
        //set each image goes here
    });
}

**Credit to Daniel Blezek Can I load a UIImage from a URL?

Community
  • 1
  • 1
Dominic
  • 258
  • 2
  • 8
  • Your going to need to use a UICollectionView or a UITableView and those are separate areas for you to investigate. – Dominic Jun 10 '16 at 09:55
  • That would still be a different question on SO. Check out cocoapods and other sources for something that you like. :) – Dominic Jun 10 '16 at 10:20
  • Check out www.cocoapods.org and www.cocoacontrols.com and make sure you vote up my answer which assists with your original question. :) – Dominic Jun 10 '16 at 10:31
  • You need to do some research. There is no quick way to get a carousel of images into an app without some knowledge and experience. Cocoapods and Cocoacontrols can save you from building your own control but there is no quick way around this. I suggest that you sleep on it and then do some more research. – Dominic Jun 10 '16 at 11:26