iPhone SDK: How come the following code only animates first frame? - ios

I have been trying to animate using an NSTimer and I've set up my code like this:
- (IBAction)startClick:(id)sender{
animationTimer = [NSTimer scheduledTimerWithTimeInterval:(.1/25.0) target:self selector:#selector(tick) userInfo:nil repeats:YES];
}
- (void)tick{
[self animatePig];
}
- (void)animatePig{
UIImage *pigImage1 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"gordonapple0004.png" ofType:nil] ];
UIImage *pigImage2 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"gordonapple0005.png" ofType:nil] ];
UIImage *pigImage3 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"gordonapple0006.png" ofType:nil] ];
UIImage *pigImage4 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"gordonapple0007.png" ofType:nil] ];
UIImage *pigImage5 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"gordonapple0008.png" ofType:nil] ];
UIImage *pigImage6 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"gordonapple0009.png" ofType:nil] ];
UIImage *pigImage7 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"gordonapple0010.png" ofType:nil] ];
UIImage *pigImage8 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"gordonapple0011.png" ofType:nil] ];
UIImage *pigImage9 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"gordonapple0012.png" ofType:nil] ];
UIImage *pigImage10 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"gordonapple0013.png" ofType:nil] ];
UIImage *pigImage11 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"gordonapple0014.png" ofType:nil] ];
UIImage *pigImage12 =[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"gordonapple0015.png" ofType:nil] ];
if(gordon.image == pigImage1)
gordon.image = pigImage2;
else if(gordon.image == pigImage2)
gordon.image = pigImage3;
else if(gordon.image == pigImage3)
gordon.image = pigImage4;
else if(gordon.image == pigImage4)
gordon.image = pigImage5;
else if(gordon.image == pigImage6)
gordon.image = pigImage7;
else if(gordon.image == pigImage7)
gordon.image = pigImage8;
else if(gordon.image == pigImage8)
gordon.image = pigImage9;
else if(gordon.image == pigImage9)
gordon.image = pigImage10;
else if(gordon.image == pigImage10)
gordon.image = pigImage11;
else if(gordon.image == pigImage11)
gordon.image = pigImage12;
else
gordon.image = pigImage12;
}
Does anyone know why this only animates the first frame? The problem's probably very simple to fix, i'm just too much of a noob in iPhone development to notice.
----------Edit----------
Okay, I have got my NSTimer to work correctly, and my next step was to stop my animation repeating over and over, so I invalidated my timer like this:
[animationTimer invalidate];
And now, when the timer is called again it doesn't work, so how would I validate it again? (re-instantiate it I think)
(BTW I Have set up another SO question for this)
My code as of now:
- (IBAction)startClick:(id)sender{
animationTimer = [NSTimer scheduledTimerWithTimeInterval:(1.00/30.00) target:self selector:#selector(tick) userInfo:nil repeats:YES];
}
- (void)tick{
[self animatePig];
}
- (void)animatePig{
UIImage *pigImage1=[UIImage imageNamed:#"gordonapple0004.png"];
UIImage *pigImage2=[UIImage imageNamed:#"gordonapple0005.png"];
UIImage *pigImage3=[UIImage imageNamed:#"gordonapple0006.png"];
UIImage *pigImage4=[UIImage imageNamed:#"gordonapple0007.png"];
UIImage *pigImage5=[UIImage imageNamed:#"gordonapple0008.png"];
UIImage *pigImage6=[UIImage imageNamed:#"gordonapple0009.png"];
UIImage *pigImage7=[UIImage imageNamed:#"gordonapple0010.png"];
UIImage *pigImage8=[UIImage imageNamed:#"gordonapple0011.png"];
UIImage *pigImage9=[UIImage imageNamed:#"gordonapple0012.png"];
UIImage *pigImage10=[UIImage imageNamed:#"gordonapple0013.png"];
UIImage *pigImage11=[UIImage imageNamed:#"gordonapple0014.png"];
UIImage *pigImage12=[UIImage imageNamed:#"gordonapple0015.png"];
UIImage *pigImage13=[UIImage imageNamed:#"gordonapple0016.png"];
if(gordon.image == pigImage1)
gordon.image = pigImage2;
else if(gordon.image == pigImage2)
gordon.image = pigImage3;
else if(gordon.image == pigImage3)
gordon.image = pigImage4;
else if(gordon.image == pigImage4)
gordon.image = pigImage5;
else if(gordon.image == pigImage5)
gordon.image = pigImage6;
else if(gordon.image == pigImage6)
gordon.image = pigImage7;
else if(gordon.image == pigImage7)
gordon.image = pigImage8;
else if(gordon.image == pigImage8)
gordon.image = pigImage9;
else if(gordon.image == pigImage9)
gordon.image = pigImage10;
else if(gordon.image == pigImage10)
gordon.image = pigImage11;
else if(gordon.image == pigImage11)
[animationTimer invalidate];
else
gordon.image = pigImage1;
}

You're going about it the hard way.
Create a UIImageView and put it on the screen.
Load your images into an NSArray.
Set the UIImageView's animationImages property to the array.
You can adjust the animationDuration and animationRepeatCount properties as well.
When user presses the start button, call the startAnimating method of the image view.
You don't need the timer nor the manual frame advancing code.

There's no good way to know without seeing more code from you. How do you store "gordon"? What type is it? presumably some type of UIImageView?
However, your real problem (if I may go to a higher level) is your entire structure. Try something more like this:
// Interface file
NSMutableArray *pigImages;
int pigImageIndex;
// Implementation file
const int firstImageIndex = 4;
const int lastImageIndex = 15;
const int imageCount = (lastImageIndex + 1 - firstImageIndex);
- (void) initPigImages
{
pigImages = [[NSMutableArray alloc] initWithCapacity: imageCount];
for (int i = firstImageIndex; i <= lastImageIndex; ++i)
{
[pigImages addObject: [self makePigImageWithIndex: i]];
}
pigImageIndex = 0;
}
- (UIImage*) makePigImageWithIndex: (int) imageIndex
{
NSString *imageName = [NSString stringWithFormat: #"gordonapple%4d.png", imageIndex]; // Formatting may be a little off.
return [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: imageName ofType: nil]]];
}
- (void )animatePig
{
if (pigImageIndex < imageCount)
{
gordon.image = pigImages[pigImageIndex++];
}
}

To restart your timer you can use -[NSRunLoop addTimer:forMode:] or just release and create a new timer each time.

Related

NSArray [UIImage setContentMode:]

I want to put defaults images if i don't have images from url feed.
if(![[stories objectAtIndex:storyIndex] valueForKey:#"url"]==nil)
{ NSNumber *numberOfKeyInstances = [stories valueForKey:#"key"];
imageArray=[NSArray arrayWithObjects:[UIImage imageNamed:#"1.png"],nil];
int randomNumber_ = rand ()%19;
UIImage *image = [imageArray objectAtIndex:indexPath.row];
cell.imag=image;
}
else{
if (![[[stories objectAtIndex:storyIndex] valueForKey:#"url"] isEqualToString:#""]) {
[cell.imag setImageWithURL:[NSURL URLWithString:[[stories objectAtIndex:storyIndex] objectForKey:#"url"]] placeholderImage:[UIImage imageNamed:#"Alb.png"]];
}
}
This is my code, but i get [UIImage setContentMode:]: unrecognized selector sent to instance 0x7f908c1dcce0'
How can i solved?
Firstly, imageArray is initialised with only one image. So calling [imageArray objectAtIndex:indexPath.row] is a problem. You dont need an image array for that. Secondly, the argument inside the first if statement doesn't look right.
Also, as #midhun-mp Pointed out, u should be probably using cell.imag.image.
Try this :
if([[stories objectAtIndex:storyIndex] valueForKey:#"url"] == nil)
{
cell.imag.image = [UIImage imageNamed:#"1.png"];
}
else if ([[[stories objectAtIndex:storyIndex] valueForKey:#"url"] isEqualToString:#""])
{
cell.imag.image = [UIImage imageNamed:#"1.png"];
}
else
{
//SET YOUR IMAGE HERE
}
Try to change
UIImage *image = [imageArray objectAtIndex:indexPath.row];
to
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:[imageArray objectAtIndex:indexPath.row]]];
I suspect that cell.imag is a UIImageView not UIImage property.
So you need to change:
cell.imag=image;
to
cell.imag.image = image;

Converting imageNamed to imageWithContentsOfFile:

My code for my images is
-(IBAction)start:(id)sender
{
animation.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"Paddle 1.png"],
[UIImage imageNamed:#"Paddle 2.png"],
[UIImage imageNamed:#"Paddle 3.png"],
[UIImage imageNamed:#"Paddle 4.png"],
nil];
[animation setAnimationRepeatCount:0];
animation.animationDuration = 2.5;
[animation startAnimating];
}
This is caching too much memory and I was told in a previous question to swap my code to using
[UIImage imageWithContentsOfFile: GetImgWithoutCaching(#"Paddle 1.jpg")]
and
UIImage* GetImgWithoutCaching(NSString* imgName)
{
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imgName ofType:nil];
return [UIImage imageWithContentsOfFile:imagePath];
}
What is the correct way of writing the code? do i place that code in my .m as is?
First you should check if using retina picture:
BOOL isHighResolution = NO;
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)]) {
if ([UIScreen mainScreen].scale > 1) {
isHighResolution = YES;
}
}
if you are using retina picture, add #2x to image name, like this:
NSString *noExtFileName = [name stringByDeletingPathExtension];
if (isHighResolution) {
if (![noExtFileName hasSuffix:#"#2x"]) {
noExtFileName = [noExtFileName stringByAppendingString:#"#2x"];
}
}
//if image only "png" type
return [[NSBundle mainBundle] pathForResource:noExtFileName ofType:#"png"];
Since your GetImgWithoutCaching function returns a UIImage you would need:
-(IBAction)start:(id)sender
{
animation.animationImages = [NSArray arrayWithObjects:
GetImgWithoutCaching(#"Paddle 1.jpg"),
GetImgWithoutCaching(#"Paddle 2.jpg"),
GetImgWithoutCaching(#"Paddle 3.jpg"),
GetImgWithoutCaching(#"Paddle 4.jpg"),
nil];
[animation setAnimationRepeatCount:0];
animation.animationDuration = 2.5;
[animation startAnimating];
}

ipad 1 Crash - my app is bad somewhere

I have made an app, but on ipad 1 it crashes in the end part. On ipad-higher it remains working, but still I think I am doing something wrong, I really need to make something working more economically. But what is it?
In the end part it all happens. Before that, several screens are put onto each other and in the simulator when you go back they all are dismissed. But on the ipad 1 you will not even reach the end of the end part. I will try to explain what happens and several idea's about what is happening I have.
You can swipe on the screen and retina images (big images) are exchanged with the four corners that can get selected, each with a different image-background. There are six different scenes, so there are 6x4 + 6x1 for-normal-0-situation, 25 big retina images. There are only 5 frames working on top of each other (4 corners and 1 background), but I set inactive stuff to nil and load active images with imageNamed. It goes well on the simulator, but it crashes on the ipad 1, with no low memory warning or what so ever.
I've studied improvements: a) autorelease pool b) imageNamed replaced by imageContentsOfFile.
The autorelease pools don't have any effect. imageNamed, I couldn't get imageContentsOfFile working, so I skipped the operation.
Any ideas of what is so excessively wrong and what kind of thoughts can be applied to have it work more economically, for it somewhere is demanding way too much in the end.
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint tappedPt = [[touches anyObject] locationInView:self.view];
NSArray *subviews = [[NSArray alloc]init];
subviews = [self.view subviews];
// Return if there are no subviews
if ([subviews count] == 0) return;
for (UIView *subview in subviews) {
if ([subview pointInside:[self.view convertPoint:tappedPt toView:subview] withEvent:event]){
[self checkAndChange:subview];
[self modifyChoice:subview];
}
}
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// Get the subviews of the view
CGPoint tappedPt = [[touches anyObject] locationInView:self.view];
NSArray *subviews = [[NSArray alloc]init];
subviews = [self.view subviews];
// Return if there are no subviews
if ([subviews count] == 0) return;
for (UIView *subview in subviews) {
if ([subview pointInside:[self.view convertPoint:tappedPt toView:subview] withEvent:event]){
[self checkAndChange:subview];
[self modifyChoice:subview];
}
}
}
-(void) modifyChoice:(UIView *)subview
{
if(subview==area1LinksBoven){[gekozenExpressieBijDimensie replaceObjectAtIndex:dimensie-1 withObject:[NSNumber numberWithInteger:1]];}
if(subview==area2RechtsBoven){[gekozenExpressieBijDimensie replaceObjectAtIndex:dimensie-1 withObject:[NSNumber numberWithInteger:2]];}
if(subview==area3RechtsOnder){[gekozenExpressieBijDimensie replaceObjectAtIndex:dimensie-1 withObject:[NSNumber numberWithInteger:3]];}
if(subview==area4LinksOnder){[gekozenExpressieBijDimensie replaceObjectAtIndex:dimensie-1 withObject:[NSNumber numberWithInteger:4]];}
if(maxChoice<dimensie){maxChoice=dimensie;
if(maxChoice==1){headerGevuld.image = [UIImage imageNamed:#"TOP1.png"];}
if(maxChoice==2){headerGevuld.image = [UIImage imageNamed:#"TOP2.png"];}
if(maxChoice==3){headerGevuld.image = [UIImage imageNamed:#"TOP3.png"];}
if(maxChoice==4){headerGevuld.image = [UIImage imageNamed:#"TOP4.png"];}
if(maxChoice==5){headerGevuld.image = [UIImage imageNamed:#"TOP5.png"];}
if(maxChoice==6){headerGevuld.image = [UIImage imageNamed:#"TOP6.png"];}
}
}
-(void) checkAndChange:(UIView *)subview{
if(subview==area1LinksBoven){
area1LinksBoven.image = [UIImage imageNamed:#"SELECTION1.png"];
area2RechtsBoven.image = nil;
area3RechtsOnder.image = nil;
area4LinksOnder.image = nil;
footer.image = [UIImage imageNamed:#"FOOT1.png"];
#autoreleasepool {
if(dimensie==1){
filmAchtergrond0.image = [UIImage imageNamed:#"1PassiefActiefOntspanning.jpg"];}
if(dimensie==2){
filmAchtergrond0.image = [UIImage imageNamed:#"1MakkelijkMoeilijkMaster.jpg"];}
if(dimensie==3){
filmAchtergrond0.image = [UIImage imageNamed:#"1VeiligGevaarlijkGeborgenheid.jpg"];}
if(dimensie==4){filmAchtergrond0.image = [UIImage imageNamed:#"1NormaalBijzonderGeaard.jpg"];}
if(dimensie==5){filmAchtergrond0.image = [UIImage imageNamed:#"1AlleenSamenUniek.jpg"];}
if(dimensie==6){filmAchtergrond0.image = [UIImage imageNamed:#"1OrdeChaosDuidelijk.jpg"];}
}
[arrowRight setImage:[UIImage imageNamed:#"ARROW_RIGHT_ACTIVE.png"] forState:UIControlStateNormal];
}
if(subview==area2RechtsBoven){
area1LinksBoven.image = nil;
area2RechtsBoven.image = [UIImage imageNamed:#"SELECTION2.png"];
area3RechtsOnder.image = nil;
area4LinksOnder.image = nil;
footer.image = [UIImage imageNamed:#"FOOT2.png"];
#autoreleasepool {
if(dimensie==1){filmAchtergrond0.image = [UIImage imageNamed:#"2PassiefActiefEnergiek.jpg"];}
if(dimensie==2){
filmAchtergrond0.image = [UIImage imageNamed:#"2MakkelijkMoeilijkUitdagend.jpg"];}
if(dimensie==3){
filmAchtergrond0.image = [UIImage imageNamed:#"2VeiligGevaarlijkSpannend.jpg"];}
if(dimensie==4){filmAchtergrond0.image = [UIImage imageNamed:#"2NormaalBijzonderWow.jpg"];}
if(dimensie==5){filmAchtergrond0.image = [UIImage imageNamed:#"2AlleenSamenGezellig.jpg"];}
if(dimensie==6){filmAchtergrond0.image = [UIImage imageNamed:#"2OrdeChaosImproviseren.jpg"];}
}
[arrowRight setImage:[UIImage imageNamed:#"ARROW_RIGHT_ACTIVE.png"] forState:UIControlStateNormal];
}
if(subview==area3RechtsOnder){
area1LinksBoven.image = nil;
area2RechtsBoven.image = nil;
area3RechtsOnder.image = [UIImage imageNamed:#"SELECTION3.png"];
area4LinksOnder.image = nil;
footer.image = [UIImage imageNamed:#"FOOT3.png"];
#autoreleasepool {
if(dimensie==1){
filmAchtergrond0.image = [UIImage imageNamed:#"3PassiefActiefUitputting.jpg"];}
if(dimensie==2){
filmAchtergrond0.image = [UIImage imageNamed:#"3MakkelijkMoeilijkFrustrerend.jpg"];}
if(dimensie==3){
filmAchtergrond0.image = [UIImage imageNamed:#"3VeiligGevaarlijkGevaarlijk.jpg"];}
if(dimensie==4){
filmAchtergrond0.image = [UIImage imageNamed:#"3NormaalBijzonderOnbegrijjpelijk.jpg"];}
if(dimensie==5){filmAchtergrond0.image = [UIImage imageNamed:#"3AlleenSamenGroepsdruk.jpg"];}
if(dimensie==6){filmAchtergrond0.image = [UIImage imageNamed:#"3OrdeChaosRommeltje.jpg"];}
}
[arrowRight setImage:[UIImage imageNamed:#"ARROW_RIGHT_ACTIVE.png"] forState:UIControlStateNormal];
}
if(subview==area4LinksOnder){
area1LinksBoven.image = nil;
area2RechtsBoven.image = nil;
area3RechtsOnder.image = nil;
area4LinksOnder.image = [UIImage imageNamed:#"SELECTION4.png"];
footer.image = [UIImage imageNamed:#"FOOT4.png"];
#autoreleasepool {
if(dimensie==1){filmAchtergrond0.image = [UIImage imageNamed:#"4PassiefActiefApathie.jpg"];}
if(dimensie==2){filmAchtergrond0.image = [UIImage imageNamed:#"4MakkelijkMoeilijkSaai.jpg"];}
if(dimensie==3){
filmAchtergrond0.image = [UIImage imageNamed:#"4VeiligGevaarlijkBeklemmend.jpg"];}
if(dimensie==4){filmAchtergrond0.image = [UIImage imageNamed:#"4NormaalBijzonderSleur.jpg"];}
if(dimensie==5){filmAchtergrond0.image = [UIImage imageNamed:#"4AlleenSamenEenzaam.jpg"];}
if(dimensie==6){filmAchtergrond0.image = [UIImage imageNamed:#"4OrdeChaosRigide.jpg"];}
}
[arrowRight setImage:[UIImage imageNamed:#"ARROW_RIGHT_ACTIVE.png"] forState:UIControlStateNormal];
}
if(dimensie==1){headerPositie.image = [UIImage imageNamed:#"OUT1.png"];}
if(dimensie==2){headerPositie.image = [UIImage imageNamed:#"OUT2.png"];}
if(dimensie==3){headerPositie.image = [UIImage imageNamed:#"OUT3.png"];}
if(dimensie==4){headerPositie.image = [UIImage imageNamed:#"OUT4.png"];}
if(dimensie==5){headerPositie.image = [UIImage imageNamed:#"OUT5.png"];}
if(dimensie==6){headerPositie.image = [UIImage imageNamed:#"OUT6.png"];}
}
Load the images using UIImage imageWithContentsOfFile:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"fileExtension"];
UIImage *img = [UIImage imageWithContentsOfFile:filePath];
See here the difference between imageNamed and imageWithContentsOfFile:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImage_Class/Reference/Reference.html

Need to see which UIImageView is empty and place an image there

Here's my problem. I have 3 UIImageViews that are empty by default, placed on a UIView that are is hidden at first.
And they are supposed to be filled one after another when a user selects something.
I know it sounds confusing, but I think my code below will clear things out:
- (IBAction)selectButtonPressed:(id)sender {
if ([labelText.text isEqualToString:#"some text"]) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"someImage"] ofType:#"png"];
UIImage * image = (UIImage * )[UIImage imageWithContentsOfFile:filePath];
if (firstImage.image == nil) {
firstImage.image = image;
imagesView.hidden = YES;
firstImage.hidden = NO;
} // places it in the first Image View if it's empty ...
else if (secondImage.image == nil) {
secondImage.image = image;
imagesView.hidden = YES;
secondImage.hidden = NO;
} // ... or in the second one if empty
else {
thirdImage.image = image;
imagesView.hidden = YES;
thirdImage.hidden = NO; // ... else in the third one
}
[filePath release];
[image release];
}
// then does the same thing depending on the users selection.
else if ([labelText.text isEqualToString:#"some other text"]) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"someOtherImage"] ofType:#"png"];
UIImage * image = (UIImage * )[UIImage imageWithContentsOfFile:filePath];
if (firstImage.image == nil) {
firstImage.image = image;
imagesView.hidden = YES;
firstImage.hidden = NO;
}
else if (secondImage.image == nil) {
secondImage.image = image;
imagesView.hidden = YES;
secondImage.hidden = NO;
}
else {
thirdImage.image = image;
imagesView.hidden = YES;
thirdImage.hidden = NO;
}
[filePath release];
[image release];
}
// and same thing below if none of the above.
else {
NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"yet another text"] ofType:#"png"];
UIImage * image = (UIImage * )[UIImage imageWithContentsOfFile:filePath];
if (firstImage.image == nil) {
firstImage.image = image;
imagesView.hidden = YES;
firstImage.hidden = NO;
}
else if (secondImage.image == nil) {
secondImage.image = image;
imagesView.hidden = YES;
secondImage.hidden = NO;
}
else {
thirdImage.image = image;
imagesView.hidden = YES;
thirdImage.hidden = NO;
}
[filePath release];
[image release];
} // Code for checking which Image View is empty and place the image there.
}
So, it launches and works fine, when I do the first selection. But for the 2nd one it says EXC_BAD_ACCESS and crashes. Sometimes it works for the first 2 selections and for the third one it crashes.
Please let me know if it's confusing, I'll try to explain my intent as clear as I can.
Thanks a lot
UPDATE: Well, I think it's true that an answer appears when you ask the right question.
Everything worked well, after I've used firstImage.image = [UIImage imageNamed:#"someImage"]; instead of the whole NSBundle method. Code became cleaner and works like a charm.
However I'm not sure if there are any downsides to this. So if you guys can warn me of anything, I'm all opened to that.
Thanks again.
Your problem probably related to your memory management. Did you use property for those imageview? Also, you don't need to release filePath and image because you did not use the alloc, etc. method to initialize it.

How do I use imageWithContentsOfFile for an array of images used in an animation?

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.

Resources