ios - 如何在 UIImageView 中加载图像数组

标签 ios iphone arrays json uiimageview

我想在单个 ImageView 中加载图像阵列(图像像图像 slider 一样一个接一个地加载)。我通过网络服务器 (JSON) 获取图像数组。我尝试了很多次,但我的应用程序崩溃了,并且多次显示错误。请帮助我如何创建图像 slider 。谢谢你

我的代码

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

最佳答案

您是从服务器获取图像 url 而不是图像本身,因此 imagearray 现在是 url 的集合,您不能直接将 url 本身设置为 animationImages。所以您接下来需要做的是您需要从图像 url 下载图像,然后将数组(将包含 UIImage 的实例)设置为 ImageView 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];
}

关于ios - 如何在 UIImageView 中加载图像数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38565452/

相关文章:

ios - 使用自定义 URL 处理程序在两个 iOS 应用程序之间移动数据/图像

C++ make函数返回数组交叉文件

python - 将 NumPy 数组按元素映射到更多维度的数组

ios - 我怎样才能普遍禁用横向?

iphone - iOS:如何将 NSData 的长度添加为其两个字节的 header ?

ios - NSURLSession:无法与后台传输服务通信

iphone - 如何从iPhone应用程序链接到iTunes store应用程序中的“特色音乐”部分?

iphone - UINavigationController - 在所有 View 中保留背景图像

iphone - 如何在 iPhone 中创建 XML 字符串以用于 Web 服务请求?

java - 在不同时间将新值累加到数组中