问题
I need to get array of images that have to call from server and arrange that in an order.But while executing below code i can't get it...Guidance please...
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
NSURL *url = [NSArray arrayWithObjects:[NSURL URLWithString:
@"http://images.wikia.com/marvelmovies/images/5/5e/The-hulk-2003.jpg"],
[NSURL URLWithString:@"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],
[NSURL URLWithString:@"http://challenge.roadef.org/2012/img/google_logo.jpg"],
[NSURL URLWithString:@"http://addyosmani.com/blog/wp-content/uploads/2012/03/Google-doodle-of-Richard-007.jpg"],
[NSURL URLWithString:@"http://techcitement.com/admin/wp-content/uploads/2012/06/apple-vs-google_2.jpg"],
[NSURL URLWithString:@"http://www.andymangels.com/images/IronMan_9_wallpaper.jpg"],
[NSURL URLWithString:@"http://sequelnews.com/wp-content/uploads/2011/11/iphone_5.jpg"],Nil];
/*
int i=0;
int cord_x=0;
int cord_y=30;
int cont=0;
for (i = 0; i < [(NSArray*)url count]; i++) {
cont=cont+1;
if (cont > 3) {
cord_x=0;
cord_y=cord_y+110;
cont=1;
}*/
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:(NSURL*)[(NSArray*)url objectAtIndex:index]]];
//cord_x=cord_x+110;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 200.0f, 200.0f);
[button setImage:(UIImage*)[image objectAtIndex:index] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return button;
}
//}
now i need to place button image and image from server...
回答1:
int row = 0;
int column = 0;
for(int i = 0; i < url.count; ++i) {
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:(NSURL*) [(NSArray*)url objectAtIndex:i]]];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(column*100+16, row*90, 80, 80);
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self
action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[self.view addSubview:button];
if (column == 2) {
column = 0;
row++;
} else {
column++;
}
}
回答2:
You can try to use NSURLConnection to download images asynchronous which will make your UI much more responsive. Just setup separate connection for each image and update images on the buttons in delegate method - connectionDidFinishLoading. It will also help you to track downloading errors (e.g. timeouts etc.). Here is a great example with downloads images for table - you can find there a lot of help for your case: http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html
回答3:
NSMutableArray *arrayList=[[NSMutableArray alloc] initWithObjects:@"www.xxx.com/imag.jpg",@"www.xxx.com/img1.jpg",nil];
for(int i=0; i<[arrayList count]; i++)
{
NSURL *url = [NSURL URLWithString:[arrayList ObjetAtIndex:i]];
NSData *data = [NSData dataWithContentsOfURL: url];
UIImage *img=[UIImage imageWithData:data]; // Here is your image
}
回答4:
NSURL *url = [NSArray arrayWithObjects:[NSURL URLWithString:@"http://mobiledevelopertips.com/images/logo-iphone-dev-tips.png"],[NSURL URLWithString:@"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],[NSURL URLWithString:@"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],[NSURL URLWithString:@"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],[NSURL URLWithString:@"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],[NSURL URLWithString:@"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],[NSURL URLWithString:@"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],Nil];
int i=0;
int cord_x=0;
int cord_y=30;
int cont=0;
for (i = 0; i < [(NSArray*)url count]; i++)
{
cont=cont+1;
if (cont > 3)
{
cord_x=0;
cord_y=cord_y+110;
cont=1;
}
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:(NSURL*)[(NSArray*)url objectAtIndex:i]]];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cord_x, cord_y, 100, 100)];
[imageView setImage:image];
[self.view addSubview:imageView];
cord_x=cord_x+110;
}
来源:https://stackoverflow.com/questions/11220795/how-to-importing-image-from-server-and-arrange-it-in-a-view