遍历来自 NSArray 的数据并从 url 中提取 UIImage

Posted

技术标签:

【中文标题】遍历来自 NSArray 的数据并从 url 中提取 UIImage【英文标题】:Looping through data from an NSArray and extracting the UIImage from a url 【发布时间】:2013-06-25 11:07:06 【问题描述】:

我有一些在线数据,我正在尝试获取图像的 URL,然后将这些图像添加到 UIPageControl 中的图像数组中。

在线[carousel setImages:[NSArray arrayWithObjects:@"1.png", @"2.png", @"3.png", nil]]; the images are set

我想设置每个图像。我遇到的问题之一是事先不知道图像的数量。此外,我得到的数据是一个字符串(图像的 url)而不是 UIImage。

for (int i = 0; i < [publicDataArray count]; i++)

    NSDictionary *dict = [publicDataArray objectAtIndex:i];
    for(NSString *key in dict) 

     NSString *someString = [[dict objectForKey:@"photo"] objectForKey:@"url"];

        NSLog(@"some string %@",someString);

    



NSLog(@"publicDataArray count %lu",(unsigned long)[publicDataArray count]);


    [carousel setImages:[NSArray arrayWithObjects:@"1.png", @"2.png", @"3.png", nil]];


    // Add carousel to view
    [self.view addSubview:carousel];

NSLog 输出:

2013-06-25 11:54:01.094 some string http://***.com/uploads/img/d/1.jpg
2013-06-25 11:54:01.094 some string http://***.com/uploads/img/d/2.jpg
2013-06-25 11:54:01.095 some string http://***.com/uploads/img/d/3.jpg

2013-06-25 11:54:01.097 publicDataArray count 3

还有我的轮播类代码.m文件:

    #import "Carousel.h"

@implementation Carousel

@synthesize pageControl;
@synthesize images;

#pragma mark - Override images setter

- (void)setImages:(NSArray *)newImages

    if (newImages != images)
    
        [newImages retain];
        [images release];
        images = newImages;

        [self setup];
    


#pragma mark - Carousel setup

- (void)setup

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
    [scrollView setDelegate:self];
    [scrollView setShowsHorizontalScrollIndicator:NO];
    [scrollView setPagingEnabled:YES];
    [scrollView setBounces:NO];

    CGSize scrollViewSize = scrollView.frame.size;

    for (NSInteger i = 0; i < [self.images count]; i++)
    
        CGRect slideRect = CGRectMake(scrollViewSize.width * i, 0, scrollViewSize.width, scrollViewSize.height);

        UIView *slide = [[UIView alloc] initWithFrame:slideRect];
        [slide setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];

//这是当前导致崩溃的原因:'-[UIImageView length]: unrecognized selector //sent to instance.

  **UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.frame];
        [imageView setImage:[UIImage imageNamed:[self.images objectAtIndex:i]]];
        [slide addSubview:imageView];
        [imageView release];**

        [scrollView addSubview:slide];
        [slide release];
    

    UIPageControl *tempPageControll = [[UIPageControl alloc] initWithFrame:CGRectMake(0, scrollViewSize.height - 20, scrollViewSize.width, 20)];
    [self setPageControl:tempPageControll];
    [tempPageControll release];
    [self.pageControl setNumberOfPages:[self.images count]];
    [scrollView setContentSize:CGSizeMake(scrollViewSize.width * [self.images count], scrollViewSize.height)];

    [self addSubview:scrollView];
    [scrollView release];
    [self addSubview:self.pageControl];


#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    [self.pageControl setCurrentPage:page];


#pragma mark - Cleanup

- (void)dealloc

    [pageControl release];
    [images release];
    [super dealloc];


@end

感谢您的帮助

【问题讨论】:

没有解决您的问题 【参考方案1】:

要在从服务器获取图像时启用应用程序并在加载图像时禁用块,请尝试使用 UIImageView+AFNetworking 库从服务器异步加载图像AFNetworking

NSMutableArray* imageViews= [[NSMutableArray alloc]init];
for (int i = 0; i < [publicDataArray count]; i++)

    NSDictionary *dict = [publicDataArray objectAtIndex:i];
    for(NSString *key in dict)
    
        NSString *imageUrl = [[dict objectForKey:@"photo"] objectForKey:@"url"];

        UIImageView *myImage = [[UIImageView alloc] init];

        [myImage setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage    imageNamed:@"PlaceHolder.png"]];

        [imageViews addObject:myImage];
       


NSLog(@"publicDataArray count %lu",(unsigned long)[publicDataArray count]);

[carousel setImages:[NSArray arrayWithArray:imageViews]];

// Add carousel to view
[self.view addSubview:carousel];

【讨论】:

谢谢!实例方法确实出现错误 - setImageWithUrl:placeholderImage:' not found (return type defaults to id' 我的错;我现在补充了。行 [carousel setImages:[NSArray arrayWithArray:imageViews]];触发未捕获的异常'NSInvalidArgumentException',原因:'-[UIImageView 长度]:无法识别的选择器发送到实例。这是数组的边界问题吗?我不确定那是什么。再次感谢:) 确保在测量长度时处理轮播中的图像数组,因为此错误表示您正在尝试测量单个图像视图的长度 再次感谢。我为上面的轮播类添加了代码。我花了几个小时在上面,但没有看到长度属性是如何发挥作用的。 @hanumanDev 如果我的回答解决了您的问题,请注意将其标记为已接受的答案:)【参考方案2】:

首先在 NSMutable 数组中获取你的 url,然后通过以下方式从 URL 中获取图像:

for (int i = 0; i < [publicDataArray count]; i++)

    NSDictionary *dict = [publicDataArray objectAtIndex:i];

    for(NSString *key in dict)
    
        NSString *someString = [[dict objectForKey:@"photo"] objectForKey:@"url"];

        NSLog(@"some string %@",someString);

        [allProfileImages addObject:someString];
    


UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[allProfileImages objectAtIndex:i]]]]];

【讨论】:

感谢您的回复!什么是对象“allProfileImages”?那是一个 NSMutableArray 吗? NSMutableArray *allProfileImages=[[NSMutableArray alloc]init]; @hanumanDev allProfileImages 是存储所有图像 URL 的数组。 我在 .h 文件中声明了 allProfileImages,因此它可以在 之外访问,尽管当我 NSLog 输出时它返回 null。 someString 不为空,并为它找到的每个项目输出值(在本例中为 url)。 allProfileImages=[[NSMutableArray alloc]init];在 viewDidLoad 方法中写下这一行。

以上是关于遍历来自 NSArray 的数据并从 url 中提取 UIImage的主要内容,如果未能解决你的问题,请参考以下文章

循环遍历 NSDictionary 以创建单独的 NSArray

Swift中实现Array数组和NSArray数组的相互转换与遍历

在 NSTableView 中显示来自 NSArray 的数据

遍历数组并从 JSON 文件中提取数据

遍历 NSArray 工作......有时

如何从 NSArray 中获取图像 URL?