如何在UIImageView中加载图像数组

时间:2016-07-25 10:34:45

标签: ios iphone arrays json uiimageview

我想在单个图像视图中对图像加载进行成像(图像逐个加载sam作为图像滑块)。我通过Web服务器(JSON)获取图像数组。我尝试了很多次,但我的应用程序崩溃,很多时间错误显示。请帮我看看如何创建图片滑块。谢谢

我的代码

-(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];
     }

1 个答案:

答案 0 :(得分:1)

您从服务器获取图像网址而不是图像本身,因此图像阵列现在是网址集合,您无法直接将网址设置为animationImages。那么接下来你需要做的是你需要从图像网址下载图像,然后将数组(包含UIImage的实例)设置为图像视图animationImages属性。

<强>更新

-(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];
}