xcode 4 setting a lot of UIImageView's - objective-c

I have 8 UIImageView in my view, all declared in the .h and .m.
I called them img01, img02, img03,..., img08
and I also have 8 pics, named 01.png, 02.png, 03.png,..., 08.png.
if I want to set the first imageView to the first pic I'll use the following command:
UIImage *image = [UIImage imageNamed:#"01.png"];
[img01 setImage:image];
What if I have a lot of UIImageViews and pics?
I tried to use this code but couldn't figure out how to do the last line of code.
int count = 1;
while (count <= 8) {
NSString *countString = [[NSString alloc]initWithFormat:#"%i", count];
NSString *picname = [NSString stringWithFormat:#"0%#.png", countString];
NSString *imgName = [NSString stringWithFormat:#"img0%#", countString];
UIImage *image = [UIImage imageNamed:picname];
[img01 setImage:image];
count++;
}
The code will only change the first imageView. I need it to change all the imageViews.

Create a NSMutableArray, and fill it with the UIImageViews. Currently you only set the first:
[img01 setImage:image];
You can do something like that:
NSMutbaleArray imageViews = [[NSMutableArray alloc] initWithCapacity:8];
for (int i = 0; i < 8; ++i) {
NSString *countString = [NSString stringWithFormat:#"%i", count];
NSString *picname = [NSString stringWithFormat:#"0%#.png", countString];
NSString *imgName = [NSString stringWithFormat:#"img0%#", countString];
UIImage *image = [UIImage imageNamed:picname];
UIImageView * view = [[[UIImageView alloc] init] autorelease];
[view setImage:image];
[imageViews addObject:view];
}

Related

UIImage Animation delay Array

i found this code which is very helpful to avoid delay at first animation:
NSMutableArray *menuanimationImages = [[NSMutableArray alloc] init];
for (int aniCount = 1; aniCount < 21; aniCount++) {
NSString *fileLocation = [[NSBundle mainBundle] pathForResource: [NSString stringWithFormat: #"bg%i", aniCount + 1] ofType: #"png"];
// here is the code to pre-render the image
UIImage *frameImage = [UIImage imageWithContentsOfFile: fileLocation];
UIGraphicsBeginImageContext(frameImage.size);
CGRect rect = CGRectMake(0, 0, frameImage.size.width, frameImage.size.height);
[frameImage drawInRect:rect];
UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[menuanimationImages addObject:renderedImage];
}
settingsBackground.animationImages = menuanimationImages;
But i want to use the same image sometimes - can i fill within the code above my array manually via
testArray = [[NSArray alloc] initWithObjects:[UIImage imageNamed:#"test_001.png"],
[UIImage imageNamed:#"test_002.png"],
[UIImage imageNamed:#"test_001.png"],
nil];
Thanks for ideas!
Yes you can by using this
UIImageView * animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200,200)];
testArray = [[NSArray alloc] initWithObjects:[UIImage imageNamed:#"test_001.png"], [UIImage imageNamed:#"test_002.png"],[UIImage imageNamed:#"test_001.png"],nil];
animTime =3.0;
[animatedImageView setAnimationImages:testArray] ;
animatedImageView.animationDuration = animTime;
animatedImageView.animationRepeatCount = 1;
[self.view addSubview: animatedImageView];
[animatedImageView startAnimating];

How to display image one by one when taking array?

I am beginner of iphone, I have all images in array but how to display one by one when I execute the array in a loop..
NSArray *images = [[NSArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
nil] retain];
int i=0;
int width = 0;
int height = 0;
for ( NSString *imageName in images )
{
//NSTimer *timer=[[NSTimer alloc]init];
UIImage *image = [images objectAtIndex:i];
//UIImage *image = [UIImage imageNamed:imageName];
animalphoto = [[UIImageView alloc] initWithImage:image];
animalphoto.contentMode = UIViewContentModeScaleAspectFit;
animalphoto.clipsToBounds = YES;
animalphoto.animationDuration=5.0;
animalphoto.animationRepeatCount=0;
animalphoto.frame = CGRectMake( animalphoto.frame.size.width * i++, 0, animalphoto.frame.size.width, animalphoto.frame.size.height);
width = animalphoto.frame.size.width;
height = animalphoto.frame.size.height;
NSLog(#"print:%#",image);
[animalphoto release];
}
}
Give any suggestion and sample code which is apply in our code..
Instead of putting one image just put whole array of images to the imgview.animationImages = images without the for loop
For further reference visit this site.
you need to use UIImageView's object animationImages to set the UIImage array
set animationRepeatCount
set animationDuration
then call [imageView startAnimating]; to start the animation

populating the views with images from array

I got 10 views on my main view, each view has a image, and i got my images in an array, how can i populate my views with images from my array. Any pointers please.
Thanks
addSubView of UIImageView to the main view and add the images to the ImageView like this
[imageView setImage:(UIImage*)[array objectAtIndex:1]];
what about putting your views into a corresponding array and looping through like this
for (in i = 0; i < [viewArray count]; i++) {
UIImageView *temp = [viewArray objectAtIndex:i];
temp.image = [imageArray objectAtIndex:i];
}
It's hard to say really, it depends on how you wanted to manage you views and getting them into an array. This method does rely on keeping track of views in array positions.
Make all UIButtons to UIButtonTypeCustom in all views and set Tag from 0 to all views
Now use this
for(int i = 0; i<[menuList count]; i++)
{
UIView *view = [self.view viewWithTag:i];
NSURL *url = [NSURL URLWithString:[menuList objectAtIndex:i]];
//NSLog(#"Hopefull MainImage: %#", url);
NSData *data = [NSData dataWithContentsOfURL:url];
//NSLog(#"data:%#", data);
UIImage *img = [[UIImage alloc] initWithData:data];
//NSLog(#"img %#", img);
UIImage *stretchableButtonImage = [img stretchableImageWithLeftCapWidth:0 topCapHeight:0];
for(int j = 0; i<[[view subViews]count]; i++)
{
[[view objectAtIndex:j] setBackgroundImage:stretchableButtonImage forState:UIControlStateNormal];
}
}

Activity Indicator while loading images inside UIScrollView

I have UIScrollView that contains images from the server.
I should put Activity Indicator while the image is currently loading.
UIScrollView contains dynamic number of images from the server.
May I know how can I add activity indicators on each page while the image is loading and remove once image is loaded.
Here's my code to retrieve images:
NSDictionary *items = [NSDictionary dictionaryWithObject:dictInfo forKey:#"images"];
imageList = [items objectForKey:#"images"];
NSArray *img = [imageList objectForKey:#"list"];
NSInteger imgCount = [img count];
buttonArray = [[NSMutableArray alloc] init];
for (int i=0; i<imgCount; i++) {
NSDictionary *imgDict = [img objectAtIndex:i];
// REQUEST FOR IMAGES
NSString *imgPath = [imgDict objectForKey:#"image_slot"];
NSString *imgURL = imgPath;
__block ASIHTTPRequest *requestImage = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:imgURL]];
[requestImage setCompletionBlock:^{
imgView = [UIImage imageWithData:[requestImage responseData]];
if (imgURL.length) {
[pendingRequests removeObjectForKey:imgURL];
}
scrollView.userInteractionEnabled = YES;
scrollView.exclusiveTouch = YES;
scrollView.canCancelContentTouches = YES;
scrollView.delaysContentTouches = YES;
scrollView.bounces = NO;
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
SWTUIButton *imgBtn = [[SWTUIButton alloc] initWithFrame:frame];
imgBtn.url = [requestImage.userInfo objectForKey:#"rURL"];
[imgBtn setImage:imgView forState:UIControlStateNormal];
imgBtn.backgroundColor = [UIColor clearColor];
[imgBtn addTarget:self action:#selector(buttonpushed:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:imgBtn];
[buttonArray addObject:imgBtn];
[imgBtn release];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * img.count, self.scrollView.frame.size.height);
}];
I highly suggest you use NINetworkImageView from https://github.com/jverkoey/nimbus project.
It's very light and useful.
It has a delegate method to let you know when an image is loaded.
What you basically need to do is:
1. create an NINetworkImageView for each page, just like you do with UIImageView, and call set
NINetworkImageView* networkImageView = [[[NINetworkImageView alloc] initWithImage:initialImage]
autorelease];
networkImageView.delegate = self;
networkImageView.contentMode = UIViewContentModeCenter;
[networkImageView setPathToNetworkImage:
#"http://farm3.static.flickr.com/2484/3929945380_deef6f4962_z.jpg"
forDisplaySize: CGSizeMake(kImageDimensions, kImageDimensions)];
https://github.com/jverkoey/nimbus/tree/master/examples/photos/NetworkPhotoAlbums
the add the indicator to the networkImageView as a subview.
implement the delegate as follows:
-(void)networkImageView:(NINetworkImageView *)imageView didLoadImage:(UIImage *)image {
[imageView removeAllSubviews];
}
the end result would be a much smaller code for doing the same thing.

How to parametrize UIView animation?

I have lot of such case statements which would fire various UIView animations. I would like to write a module and parametrize these so I don't have to repeat similar code over and over. What is the best way to pass (parametrized) values for animationRepeatCount & animationDuration?
case 10: //Animation Number 10
//.........
imageAnimation2.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"monkey_dancing_01.png"],
[UIImage imageNamed:#"monkey_dancing_02.png"],
[UIImage imageNamed:#"monkey_dancing_03.png"],
[UIImage imageNamed:#"monkey_dancing_04.png"],
nil];
imageAnimation2.animationRepeatCount = 4;
imageAnimation2.animationDuration = 2;
[imageAnimation2 startAnimating];
[self loadSoundEffectAudio:#"Monkey_10"];
[self performSelector:#selector(stopSoundEffectAudio) withObject:nil afterDelay:4];
break;
Depending on how regular your assets and animations are, you could make some utility methods to handle much of that.
Like say:
+ (void) loadAnimation:(UIImageView *)imageView name:(NSString *)filename
{
NSMutableArray * array = [[NSMutableArray alloc] init];
UIImage * image;
int index = 1;
NSString * string;
do
{
string = [NSString stringWithFormat:#"%#%04d.png", filename, index++];
image = [UIImage imageNamed:string];
if(image)
[array addObject:image];
} while (image != nil);
imageView.animationImages = array;
imageView.animationDuration = [array count] / 18;
[array release];
}
The above code loads up animations into an array with very regular naming conventions (XXXX.png). They are also all based on the same framerate so I can build the duration at the same time.