I am trying to so a simple UIImageView animation. I have 11 images of .png type. The animation is just a rabbit blinking. I am having issues with the array:
UIImageView *animationView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2-123/2, self.view.bounds.size.height/2-278/2, 123, 278)];
NSArray *imageNames = [NSArray arrayWithObjects:#"rabbit-1.png",#"rabbit-2.png",#"rabbit-3.png",#"rabbit-4.png",#"rabbit-5.png",#"rabbit-6.png",#"rabbit-7.png",#"rabbit-8.png",#"rabbit-9.png",#"rabbit-10.png",#"rabbit-11.png", nil];
NSMutableArray *images = [[NSMutableArray alloc] init];
for(int x =0; x==[imageNames count]; x++){
UIImage *image = [UIImage imageNamed: [imageNames objectAtIndex:x]];
[images addObject:image];
}
NSLog(#"image name array length is : %d",[images count]);
animationView.animationImages=images;
animationView.animationDuration=10;
[self.view addSubview:animationView];
[animationView startAnimating];
Nothing is appearing on the screen because the images array is empty. Not sure why this is. I did init and alloc in it but when I check the length it comes back as zero. Any ideas what is going on here? Any pointers would be appreciated. Thanks
x == [imageNames count] is always false, so the for loop won't get executed.
Change the line
for(int x =0; x==[imageNames count]; x++)
to
for(int x = 0; x < [imageNames count]; x++)
Related
I would like to know based on my code, how can i add fade in fade out effect between each images in my NSArray. As i am using these images as background. Changing background without fade in and out looks horrible.
Here is my code.
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *imageNames = #[#"1.jpg", #"2.png", #"3.png", #"4.png",
#"5.png"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++) {
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
// Normal Animation
UIImageView *animationImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
animationImageView.animationImages = images;
animationImageView.animationDuration = 9;
[self.background addSubview:animationImageView];
[animationImageView startAnimating];}
I am using this code to generate random image to UIImageView. When I click the button, it apears another image. I would like to ask you how to create four imageviews - click button -> apears four different random images.
ViewController.h
#interface ViewController : UIViewController {
IBOutlet UIImageView *imageView;
// IBOutlet UIImageView *imageView2;
}
-(IBAction) randomImage;
ViewController.m
- (IBAction) randomImage {
NSArray *images = [[NSArray alloc] initWithObjects:#"image1.jpg", #"image2.jpg", #"image3.jpg", #"image4.jpg", nil];
int count = [images count]
int index = arc4random() % count;
imageView.image = [UIImage imageNamed:[images objectAtIndex:index]];
// imageView2.image = ....
}
I created four imageViews in storyboard and I was trying something like above // imageView2.image... but it it not the right solution :-)
Thank you for your help
Code to list 4 random image:
NSMutableArray *images = [[NSMutableArray alloc] initWithArray: #[#"image1.jpg", #"image2.jpg", #"image3.jpg", #"image4.jpg"]];
int countImg = images.count;
for (int i = 0; i < countImg; i++) {
NSInteger index = arc4random() % images.count;
NSLog(#"Image%d name = %#",i , [images objectAtIndex:index]);
[images removeObjectAtIndex:index];
}
Rather than using IBOutlet UIImageView *imageView;, use IBOutletCollection(UIImageView) NSArray *imageViews; and connect all of your image views to this.
Then, in your code:
- (IBAction) randomImage {
NSArray *images = [[NSArray alloc] initWithObjects:#"image1.jpg", #"image2.jpg", #"image3.jpg", #"image4.jpg", nil];
NSInteger count = [images count];
for (UIImageView *imageView in self.imageViews) {
NSInteger index = arc4random() % count;
imageView.image = [UIImage imageNamed:[images objectAtIndex:index]];
}
}
I have a UIImageView on my storyboard, which is linked to an outlet in the code. When I run the code, it only shows the static picture that the storyboard shows, not the animation that I create programmatically. Any ideas?
for(int i=0; i<4; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"loading%d",i]];
[self.images addObject:image];
}
self.loadingView.animationImages = self.images;
self.loadingView.animationDuration = 2;
self.loadingView.animationRepeatCount = 1;
[self.loadingView startAnimating];
Note: loadingView is the UIImageView, images is an NSMutableArray.
If you want to make run time array you can use like this way.
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 1 ; i < 4; i++) {
[images addObject:[UIImage imageNamed:[NSString stringWithFormat: #"loading%d.jpg", i]]];
}
self.loadingView.animationImages = images;
self.loadingView.animationDuration = 2;
self.loadingView.animationRepeatCount = 1;
[self.loadingView startAnimating];
Hope this will help you. It's working fine for me.
I recommend you to use array directly like this.
NSArray *images = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"1.jpg"],
[UIImage imageNamed:#"2.jpg"],
nil];
self.loadingView.animationImages = images;
self.loadingView.animationDuration = 2;
self.loadingView.animationRepeatCount = 1;
[self.loadingView startAnimating];
I've downloaded animated bonfire from the internet. My image sequence is "*.png" and I can't get it work. It keeps looping to the end. It does not show image. How come I can't animate this code? What did I do wrong?
UIImageView* campFireView = [[UIImageView alloc] initWithFrame:screenx.frame];
int j;
int i;
for (j = 1, i = 0; i < 28; i =1)
{
NSLog( #"%i.png", i);
campFireView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:[NSString stringWithFormat:#"%i.png",i]], nil];
}
// all frames will execute in 1.75 seconds
campFireView.animationDuration = .15;
// repeat the annimation forever
campFireView.animationRepeatCount = 1;
// start animatinganimationRepeatCount:1];
[campFireView startAnimating];
// add the animation view to the main window
[self.view addSubview:campFireView];
[campFireView release];
Every time you call:
campFireView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:[NSString stringWithFormat:#"%i.png",i]], nil];
You are setting the animationImages to a one-item array. What you probably meant to do instead:
NSMutableArray *animationImages = [NSMutableArray array];
for (i = 0; i < 28; i++)
{
[animationImages addObject:[UIImage imageNamed:[NSString stringWithFormat:#"%i.png",i]]];
}
campfireView.animationImages = animationImages;
Also - I don't know what was going on with your for loop but I fixed it.
When u r looping then providing one image every time to animationImages
NSMutableArray *arrImgs = [NSMutableArray array];
for (i = 0; i < 28; i++)
{
[arrImgs addObject:[UIImage imageNamed:[NSString stringWithFormat:#"%i.png",i]]];
}
campfireView.animationImages = [NSArray arrayWithArray:arrImgs];
This is what I currently have:
NSMutableArray *images = [[NSMutableArray alloc] initWithCapacity:21];
for(int count = 1; count <= 21; count++)
{
NSString *fileName = [NSString stringWithFormat:#"dance2_%03d.jpg", count];
UIImage *frame = [UIImage imageNamed:fileName];
[images addObject:frame];
}
UIImage imageNamed is causing me some memory issues and I would like to switch to imageWithContentsOfFile.
I can make it work with a single image, but not the whole array:
NSMutableArray *images = [[NSMutableArray alloc] initWithCapacity:21];
for(int count = 1; count <= 21; count++)
{
NSString *fileName = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:#"/dance2_001.jpg"];
UIImage *frame = [UIImage imageWithContentsOfFile:fileName];
[images addObject:frame];
}
Any help is greatly appreciated! Thanks!
for(int i = 1; i <= 21; i++)
{
[images addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"dance2_%03d", i] ofType:#"jpg"]]];
}
what you should do first is create an array with the images for your animation like something like this:
NSMutableArray* images = [[NSMutableArray alloc] initWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"image1" ofType:#"jpg"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"image2" ofType:#"jpg"]],
nil];
then you can add it to an UIImageView to animate it like this:
UIImageView* animationImagesView = [[UIImageView alloc] initWithFrame:CGRectMake(posX, posY, frameWidth, frameHeight)];
animationImagesView.animationImages = images; //array of images to be animate
animationImagesView.animationDuration = 1.0; //duration of animation
animationImagesView.animationRepeatCount = 1; //number of time to repeat animation
[self.view addSubview:animationImagesView];
now you can start and stop the animation using these two calls:
[animationImagesView startAnimating]; //starts animation
[animationImagesView stopAnimating]; //stops animation
hope this helps. also remember to release and nil your Array and UIImageView when done.