Objective-C Animating a character - ios

For starters I am trying to animate my main character of the game so I can later replace the images in the array with sprites -
Ive done this in my header file for my main character :
- (void)Animate;
And in my implementation file ive written :
-(void) Animate{
UIImage* img1 = [UIImage imageNamed:#"Obstacle.png"];
UIImage* img2 = [UIImage imageNamed:#"Obstacle02.png"];
UIImage* img3 = [UIImage imageNamed:#"Obstacle03.png"];
UIImage* img4 = [UIImage imageNamed:#"Obstacle04.png"];
NSArray *images = [NSArray arrayWithObjects:img1,img2,img3,img4, nil];
UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 160.0, 160.0)];
//images.name = #"animation";
[imageView setAnimationImages:images];
[imageView setAnimationRepeatCount:0];
[imageView setAnimationDuration:0.5];
//imageView.center = character.center ;
[imageView startAnimating];
// character used to be myView
// the images inside the array !
}
Take note I am using the game feature of Xcode .
So what am I not doing and how can I fix it ?

UIImage* img1 = [UIImage imageNamed:#"Obstacle"];
UIImage* img2 = [UIImage imageNamed:#"Obstacle02"];
UIImage* img3 = [UIImage imageNamed:#"Obstacle03"];
UIImage* img4 = [UIImage imageNamed:#"Obstacle04"];
NSArray *animateArray = [NSArray arrayWithObjects:img1,img2,img3,img4, nil];
self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 160, 160)];
self.imageView.animationImages = animateArray;
self.imageView.animationDuration = 3.0;
[self.imageView startAnimating];
self.imageView.animationRepeatCount = 0;
[self.view addSubview:self.imageView];

Related

UIImage animatedImageNamed repeat count

I have a images sequence animated with :
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 120)];
[UIImage animatedImageNamed:#"spinner-" duration:1.0f];
imgView.contentMode = UIViewContentModeCenter;
imgView.animationRepeatCount = 1; // No effect
[self.view addSubView:imgView];
But the sequence keeps looping. Is there a way to control the repeat count (and keep displaying the last image) ?
In your case, using [UIImage animatedImageNamed:#"spinner-" duration:1.0f]; won't give you what you want.
For what you would like to achieve, I would recommend using UIImageView.
Usage:
First create an UIImageView object:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:<FRAME_SIZE>];
Second, create an array that holds your images to be animated.
If you just have a few, do the following:
NSMutableArray *animatedImagesArray = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:#"spinner-1.png"], [UIImage imageNamed:#"spinner-2.png"], nil];
If you have more, then you could load your images into the array like this:
for (int i = 0; i < <NUM_OF_IMAGE_COUNT>; i++)
{
[animatedImagesArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"spinner-%d%#", i, #".png"]]];
}
Once all that is done, then we configure the controls you want:
imageView.animationImages = animatedImagesArray;
imageView.animationDuration = 1.0f;
imageView.animationRepeatCount = 1.0f;
Then start animating it by:
[imageView startAnimating];
And stop the animation via:
[imageView stopAnimating];
If you would like the UIImageView to hold the last image after stopping, we would need to carry out the following before starting the animation:
imageView.image = [imageView.animationImages lastObject];
....
[imageView startAnimating];
Hope this helps.
You set the repeat count in the UIImageView object that displays your image.
UIImage *testImage = [UIImage animatedImageNamed:#"Box.jpg" duration:1.0f];
UIImageView *imgView = [[UIImageView alloc] initWithImage:testImage];
[imgView setAnimationRepeatCount:1];

How would I use a string of image names to use in a UIImageView?

I keep ending with an error saying that there are incompatible pointers assigning UIIMAGE to NSString.
self.imageNames = [NSArray arrayWithObjects:#"shop_deal.png",#"Date.png",#"Recreation.png",#"Productivity.png",nil];
UIImageView *imgView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 150)];
imgView.image = [UIImage imageNamed:_imageNames];
[self addSubview:imgView];
I'm doing something wrong here...
In this line:
imgView.image = [UIImage imageNamed:_imageNames];
you have to use an NSString, not an entire NSArray. So try:
imgView.image = [UIImage imageNamed:[_imageNames objectAtIndex:0]];
instead.

table view displaying image which is in project

i am trying to add image which i added in project i use following code for it
UIView *selectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 7, 320, 50)];
[selectionView setBackgroundColor: [UIColor clearColor]];
UIImage *image = [[UIImage alloc]init];
image = [UIImage imageWithContentsOfFile:#"PlaceHolder.png"];
UIImageView* blockView = [[UIImageView alloc] initWithImage:image];
blockView.frame = CGRectMake(0, -5, 45, 45);
if([[[_arrayForTable valueForKey:#"URL"] objectAtIndex:[indexPath row]] isEqualToString:#"No Image"] )
{
[selectionView addSubview:blockView];
}
else
{
NSString *path = [[_arrayForTable valueForKey:#"URL"] objectAtIndex:[indexPath row]];
NSURL *url = [NSURL URLWithString:path];
AsyncImageView *imageView = [[[AsyncImageView alloc] init] autorelease];
imageView.frame = CGRectMake(0, -5, 45, 45);
imageView.imageURL=url;
[selectionView addSubview:imageView];
}
else condition work perfectly they have url of facebook to display images in table view but if condition in not working it going in to if condition but image is not getting displayed plz help
UIImage *image = [UIImage imageNamed:#"PlaceHolder.png"];
UIImageView* blockView = [[UIImageView alloc] initWithImage:image];
/*
imageNamed: is to load the picture which is in bundle. pass in the picture name
imageWithContentsOfFile: need to pass in the whole address of the file
*/
Try
UIImage *image = [UIImage imageNamed:#"yourfile.png"];
and add the image to the imageView and check the image file is aded to your project

How can i add image names into an array?

In my app, i have a little problem in memory issue.
My coding skill is not enough to perfect.
My code use memory four time that i really need.
How can i change my coding?
My Code is
-(void)viewDidAppear:(BOOL)animated
{
UIScrollView * ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height-88)];// 44(UpperNavigtionBar),-88(Upper+Lower)
ScrollView.pagingEnabled = YES;
// Create a UIImage to hold Info.png
UIImage *image1 = [UIImage imageNamed:#"Image-001.jpg"];
UIImage *image2 = [UIImage imageNamed:#"Image-002.jpg"];
UIImage *image3 = [UIImage imageNamed:#"Image-003.jpg"];
UIImage *image4 = [UIImage imageNamed:#"Image-004.jpg"];
UIImage *image5 = [UIImage imageNamed:#"Image-005.jpg"];
UIImage *image6 = [UIImage imageNamed:#"Image-006.jpg"];
UIImage *image7 = [UIImage imageNamed:#"Image-007.jpg"];
UIImage *image8 = [UIImage imageNamed:#"Image-008.jpg"];
UIImage *image9 = [UIImage imageNamed:#"Image-009.jpg"];
UIImage *image10 = [UIImage imageNamed:#"Image-010.jpg"];
UIImage *image11 = [UIImage imageNamed:#"Image-011.jpg"];
UIImage *image12 = [UIImage imageNamed:#"Image-012.jpg"];
UIImage *image13 = [UIImage imageNamed:#"Image-013.jpg"];
NSArray *images = [[NSArray alloc] initWithObjects:image1,image2,image3,image4,image5,image6,image7,image8,image9,image10,image11,image12,image13,nil];
NSInteger numberOfViews = 13;
for (int i = 0; i < numberOfViews; i++)
{
CGFloat xOrigin = i * self.view.frame.size.width;
UIImageView * ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height-88)]; // -88(Upper+Lower)
[ImageView setImage:[images objectAtIndex:i]];
[ScrollView addSubview:ImageView];
}
ScrollView.contentSize = CGSizeMake(self.view.frame.size.width*numberOfViews, self.view.frame.size.height-88); // -88(for adding Image View as subview)
[self.view addSubview:ScrollView];
}
There are several things you can do to limit your memory usage. First, you should fill your array with the names of the images, not the images themselves. Given the way your images are named, you should be able to do this in a loop. Second, when you load the image, use imageWithContentsOfFile: instead of imageNamed:. The latter method caches images, while the former doesn't. If you use imageNamed: the memory usage will continue to grow as you scroll through images (even with lazy loading) because of the caching -- at some point the system should purge the cache, which should keep your app from crashing, but I don't think it will keep the system for shutting down other programs that are in the background on a device.

How to add UIImages like animation using NSTimer?

I want to add a array of UIImages to UIImageView using NSTimer. Such that it will be like an animation. How to do it in iOS?
Thanks in advance.
There is a very simple way to do it.
// create the view that will execute our animation
UIImageView* campFireView = [[UIImageView alloc] initWithFrame:self.view.frame];
// load all the frames of our animation
campFireView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"campFire01.gif"],
[UIImage imageNamed:#"campFire02.gif"],
[UIImage imageNamed:#"campFire03.gif"],
[UIImage imageNamed:#"campFire04.gif"],
[UIImage imageNamed:#"campFire05.gif"],
[UIImage imageNamed:#"campFire06.gif"],
[UIImage imageNamed:#"campFire07.gif"],
[UIImage imageNamed:#"campFire08.gif"],
[UIImage imageNamed:#"campFire09.gif"],
[UIImage imageNamed:#"campFire10.gif"],
[UIImage imageNamed:#"campFire11.gif"],
[UIImage imageNamed:#"campFire12.gif"],
[UIImage imageNamed:#"campFire13.gif"],
[UIImage imageNamed:#"campFire14.gif"],
[UIImage imageNamed:#"campFire15.gif"],
[UIImage imageNamed:#"campFire16.gif"],
[UIImage imageNamed:#"campFire17.gif"], nil];
// all frames will execute in 1.75 seconds
campFireView.animationDuration = 1.75;
// repeat the annimation forever
campFireView.animationRepeatCount = 0;
// start animating
[campFireView startAnimating];
// add the animation view to the main window
[self.view addSubview:campFireView];
Source: http://web.archive.org/web/20120305060003/http://appsamuck.com/day2.html

Resources