我想在单个 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/