我有一些在线数据,我正在尝试获取图像的网址,然后将这些图像添加到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]:无法识别的选择器//发送到实例。
**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
感谢您的帮助
答案 0 :(得分:1)
首先在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]]]]];
答案 1 :(得分: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];