1

I want to image array load in single image view (Images load one by one sam as image slider). I get image array by web server(JSON). I trying to many times but my application is crashed and many time error show. Please help me how to create image slider. Thank You

MY Code

-(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"];
NSArray *imagearray = [results valueForKey:@"slider_image_path"];
NSLog(@"images %@",imagearray);

 self.imageview.animationImages = imagearray;
    _imageview.animationDuration = 10;
    _imageview.animationRepeatCount = 0;
    [_imageview startAnimating];
     }
3
  • where u want to load Commented Jul 25, 2016 at 10:36
  • I want a image slider Commented Jul 25, 2016 at 10:37
  • What error you are getting while the app is crash? Have you checked the contains of imagearray? Commented Jul 25, 2016 at 10:42

1 Answer 1

1

You are getting the image urls from the server and not the images itself, so the imagearray is a collection of urls right now, and you can not directly set the url itself to the animationImages. So what you need to do next is you would need to download the images from the image urls and then set the array (that would contain the instances of UIImage) to the image views animationImages property.

Update

-(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"];
    NSArray *imageUrlArray = [results valueForKey:@"slider_image_path"];
    NSLog(@"images %@",imagearray);

    NSMutableArray *arrayImages = [[NSMutableArray alloc] init];

    //Updated for loop, previously in hurry i was setting url string as a url to the URLWithString method, now its working fine.
    for (NSString *strImageUrl in imageUrlArray) {
        [arrayImages addObject:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strImageUrl]]]];
    }

    self.imageview.animationImages = arrayImages;
    _imageview.animationDuration = 10;
    _imageview.animationRepeatCount = 0;
    [_imageview startAnimating];
}
Sign up to request clarification or add additional context in comments.

4 Comments

If you are talking about the time it takes to download the images from server then it depends on the internet speed, as the images you are download are large in size to it will definitely take some time. Till then what you can do is, you can show the activity indicator to the user till the images are being downloaded.
Bro i can't help you line by line or write the whole code for you, you would also need to take some efforts.
I had some other work so couldn't able to work on the issue, will help you tomorrow if got time.
Download the MBProgressHUD library from github.com/jdg/MBProgressHUD link, its very easy to use. Show the activity indicator before you make any request and hide it when you receives the response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.