I want my app to play an animation when a press a button however I want the speed of the animation to slow down halfway through? Currently my app plays a sound/animation when I press a button, however the last 50% of the frames are too fast and I want it to slow down.
Here is my code:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController {
}
- (IBAction)Button {
AudioServicesPlaySystemSound(SoundID);
}
-(IBAction)startanimating{
animatedimage.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"frames_00.png"],
[UIImage imageNamed:#"frames_05.png"],
[UIImage imageNamed:#"frames_10.png"],
[UIImage imageNamed:#"frames_15.png"],
[UIImage imageNamed:#"frames_20.png"],
[UIImage imageNamed:#"frames_25.png"],
[UIImage imageNamed:#"frames_30.png"],
[UIImage imageNamed:#"frames_35.png"],
[UIImage imageNamed:#"frames_40.png"],
[UIImage imageNamed:#"frames_45.png"],
[UIImage imageNamed:#"frames_50.png"],
[UIImage imageNamed:#"frames_50.png"],
[UIImage imageNamed:#"frames_45.png"],
[UIImage imageNamed:#"frames_40.png"],
[UIImage imageNamed:#"frames_35.png"],
[UIImage imageNamed:#"frames_30.png"],
[UIImage imageNamed:#"frames_25.png"],
[UIImage imageNamed:#"frames_20.png"],
[UIImage imageNamed:#"frames_15.png"],
[UIImage imageNamed:#"frames_10.png"],
[UIImage imageNamed:#"frames_05.png"],
[UIImage imageNamed:#"frames_00.png"],nil];
[animatedimage setAnimationRepeatCount:1];
animatedimage.animationDuration = 0.7;
[animatedimage startAnimating];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *buttonURL = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:#"AVTREV" ofType:#"mp3"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)buttonURL, &SoundID);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Frame 50 is where I want it to play at a much slower speed. Any help would be fantastic thank you!
With the current code you have an array with 22 items in it. To achieve the desired effect, you can use an array with 33 items in it, by duplicating each item after frame_50, so that the entries in the array look like this
00 05 10 ... 45 50 50 50 45 45 40 40 ... 05 05 00 00
Since the filenames follow a predictable pattern, you can even use a loop to create the array.
int i;
NSString *name;
NSMutableArray *tempArray = [NSMutableArray new];
for ( i = 0; i <= 50; i += 5 )
{
name = [NSString stringWithFormat:#"frames_%02d.png", i];
[tempArray addObject:[UIImage imageNamed:name]];
}
for ( i = 50; i >= 0; i -= 5 )
{
name = [NSString stringWithFormat:#"frames_%02d.png", i];
[tempArray addObject:[UIImage imageNamed:name]];
[tempArray addObject:[UIImage imageNamed:name]]; // add the frame twice
}
animatedImage.animationImages = tempArray;
edit -- The code above is intended to replace the array initialization code
animatedimage.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"frames_00.png"],
[UIImage imageNamed:#"frames_05.png"],
...
Related
My code used to work perfectly when I used to just have an animation. However, now I tried to add sound to the animation also, I've implemented the sound as well as I can however now every time the app runs it shuts down and gives me the SIGABRT error, here is my code:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController {
}
- (IBAction)Button {
AudioServicesPlaySystemSound(SoundID);
}
-(IBAction)startanimating{
animatedimage.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"ALambo1.jpg"],
[UIImage imageNamed:#"ALambo2.jpg"],
[UIImage imageNamed:#"ALambo3.jpg"],
[UIImage imageNamed:#"ALambo4.jpg"],
[UIImage imageNamed:#"ALambo5.jpg"],
[UIImage imageNamed:#"ALambo6.jpg"],
[UIImage imageNamed:#"ALambo7.jpg"],
[UIImage imageNamed:#"ALambo8.jpg"],
[UIImage imageNamed:#"ALambo9.jpg"],
[UIImage imageNamed:#"ALambo10.jpg"],
[UIImage imageNamed:#"ALambo11.jpg"],
[UIImage imageNamed:#"ALambo12.jpg"],
[UIImage imageNamed:#"ALambo13.jpg"],
[UIImage imageNamed:#"ALambo14.jpg"],
[UIImage imageNamed:#"ALambo15.jpg"],
[UIImage imageNamed:#"ALambo16.jpg"],
[UIImage imageNamed:#"ALambo17.jpg"],
[UIImage imageNamed:#"ALambo18.jpg"],
[UIImage imageNamed:#"ALambo19.jpg"],
[UIImage imageNamed:#"ALambo20.jpg"],
[UIImage imageNamed:#"ALambo22.jpg"],
[UIImage imageNamed:#"ALambo23.jpg"],
[UIImage imageNamed:#"ALambo24.jpg"],
[UIImage imageNamed:#"ALambo25.jpg"],
[UIImage imageNamed:#"ALambo26.jpg"],
[UIImage imageNamed:#"ALambo52.jpg"],
[UIImage imageNamed:#"ALambo51.jpg"],
[UIImage imageNamed:#"ALambo50.jpg"],
[UIImage imageNamed:#"ALambo49.jpg"],
[UIImage imageNamed:#"ALambo48.jpg"],
[UIImage imageNamed:#"ALambo47.jpg"],
[UIImage imageNamed:#"ALambo46.jpg"],
[UIImage imageNamed:#"ALambo45.jpg"],
[UIImage imageNamed:#"ALambo44.jpg"],
[UIImage imageNamed:#"ALambo43.jpg"],
[UIImage imageNamed:#"ALambo42.jpg"],
[UIImage imageNamed:#"ALambo41.jpg"],
[UIImage imageNamed:#"ALambo40.jpg"],
[UIImage imageNamed:#"ALambo39.jpg"],
[UIImage imageNamed:#"ALambo38.jpg"],
[UIImage imageNamed:#"ALambo37.jpg"],
[UIImage imageNamed:#"ALambo36.jpg"],
[UIImage imageNamed:#"ALambo35.jpg"],
[UIImage imageNamed:#"ALambo34.jpg"],
[UIImage imageNamed:#"ALambo33.jpg"],
[UIImage imageNamed:#"ALambo32.jpg"],
[UIImage imageNamed:#"ALambo31.jpg"],
[UIImage imageNamed:#"ALambo30.jpg"],
[UIImage imageNamed:#"ALambo29.jpg"],
[UIImage imageNamed:#"ALambo28.jpg"], nil];
[animatedimage setAnimationRepeatCount:1];
animatedimage.animationDuration = 1.2;
[animatedimage startAnimating];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *buttonURL = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:#"SoundRev" ofType:#"m4a"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)buttonURL, &SoundID);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
Here is the (.h) also:
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#interface ViewController : UIViewController
{
IBOutlet UIImageView *animatedimage;
SystemSoundID SoundID;
}
-(IBAction)startanimating;
#end
I haven't used AudioServicesCreateSystemSoundID in quite a while. I suggest switching your code to use AVAudioPlayer instead. That API is both more flexible and easier to use.
I have the following code to play animation with images:
-(void)playPopAnimation:(void (^)(void))completion{
__block NSMutableArray *images;
float animationDuration = 1.0f;
images = [[NSMutableArray alloc] initWithCapacity:26];
for(int i=1; i<26; i++){
NSString *fileName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"bulut_pop%d",i] ofType:#"png"];
UIImage *image = [UIImage imageWithContentsOfFile:fileName];
//UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:#"bulut_pop%d.png",i]];
[images addObject:image];
}
[Helper playSound:#"button_pop"];
UIImageView* imageView = [[UIImageView alloc] initWithFrame:self.itemImage.frame];
imageView.animationImages = images;
imageView.animationDuration = animationDuration;
imageView.animationRepeatCount = 1; // 0 = nonStop repeat
[self addSubview:imageView];
[imageView startAnimating];
self.imageButton.userInteractionEnabled = NO;
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, animationDuration * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
//[self.delegate myItemCell:self cellDidTappedFor:[self item]];
self.imageButton.userInteractionEnabled = YES;
images = nil;
});
Each time I call this function, memory increases by 10 MB. What is the good way to play animated image views?
Each time you call this method, you add a new image view (with its array of images) to your view. You should refactor your code so that the creation of the array and imageView are inside an if clause testing whether imageView is nil, so that part only runs the first time the method is called (and make imageView a property).
is it possible to manage multiple NSArrays on a different View Controller?
Here's my example:
I am currently working with two View Controllers:
TableViewController serves as a main menu. I've set up multiple UIButtons, each has a tag and when the user presses a button, it'll take them to the TableOneViewController.
In TableOneViewController, so far I've set up an UIImageView and a UIButton, every time the user presses the button, the UIImageView changes the image through an NSArray.
I want to use the tags on the TableViewController to manage the NSArrays of the TableOneViewController. Is this possible? So far, my code is not working.
Here's the code. Thanks!
TableViewController.m
- (IBAction)tableSelection:(id)sender
{
TableOneViewController *TOVC = [self.storyboard instantiateViewControllerWithIdentifier:#"TableOneViewController"];
if ([sender tag] == 1) {
[[NSUserDefaults standardUserDefaults] setObject:TOVC.numOne forKey:#"num"];
[[NSUserDefaults standardUserDefaults] setObject:#"1.png" forKey:#"num2"];
}
if ([sender tag] == 2) {
[[NSUserDefaults standardUserDefaults] setObject:TOVC.numTwo forKey:#"num"];
[[NSUserDefaults standardUserDefaults] setObject:#"2.png" forKey:#"num2"];
}
if ([sender tag] == 3) {
[[NSUserDefaults standardUserDefaults] setObject:TOVC.numThree forKey:#"num"];
[[NSUserDefaults standardUserDefaults] setObject:#"3.png" forKey:#"num2"];
}
[self presentViewController:TOVC animated:YES completion:nil];
}
TableOneViewController.m
- (IBAction)nextButton:(id)sender
{
int index = tableCounter++ % [self.numOne count];
self.num.image = self.numOne[index];
}
- (void)viewDidLoad
{
self.num.image = [UIImage imageNamed:[[NSUserDefaults standardUserDefaults] objectForKey:#"num"]];
self.num2.image = [UIImage imageNamed:[[NSUserDefaults standardUserDefaults] objectForKey:#"num2"]];
[super viewDidLoad];
tableCounter = 10;
self.numOne = [NSArray arrayWithObjects:
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
[UIImage imageNamed:#"6.png"],
[UIImage imageNamed:#"7.png"],
[UIImage imageNamed:#"8.png"],
[UIImage imageNamed:#"9.png"],
[UIImage imageNamed:#"10.png"],
[UIImage imageNamed:#"1.png"],
nil];
self.numTwo = [NSArray arrayWithObjects:
[UIImage imageNamed:#"20.png"],
[UIImage imageNamed:#"30.png"],
[UIImage imageNamed:#"40.png"],
[UIImage imageNamed:#"50.png"],
[UIImage imageNamed:#"60.png"],
[UIImage imageNamed:#"70.png"],
[UIImage imageNamed:#"80.png"],
[UIImage imageNamed:#"90.png"],
[UIImage imageNamed:#"100.png"],
[UIImage imageNamed:#"10.png"],
nil];
self.numThree = [NSArray arrayWithObjects:
[UIImage imageNamed:#"200.png"],
[UIImage imageNamed:#"300.png"],
[UIImage imageNamed:#"400.png"],
[UIImage imageNamed:#"500.png"],
[UIImage imageNamed:#"600.png"],
[UIImage imageNamed:#"700.png"],
[UIImage imageNamed:#"800.png"],
[UIImage imageNamed:#"900.png"],
[UIImage imageNamed:#"1000.png"],
[UIImage imageNamed:#"100.png"],
nil];
}
The problem is that you pass the array before presenting the view controller (viewDidLoad hasn't be called yet), then you present it, but you initialize numOne (and the other arrays) in viewDidLoad. I suggest to use lazy initialization, here's the example for numOne, write the same for the other two arrays:
- (NSArray*) numOne
{
if(!_numOne)
{
_numOne= [NSArray arrayWithObjects:
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
[UIImage imageNamed:#"6.png"],
[UIImage imageNamed:#"7.png"],
[UIImage imageNamed:#"8.png"],
[UIImage imageNamed:#"9.png"],
[UIImage imageNamed:#"10.png"],
[UIImage imageNamed:#"1.png"],
nil];
}
return _numOne;
}
However this is not the purpose of NSUserDefaults, I consider this approach bad design. It's better if you keep an ivar in TableOneViewController that points to one of the three arrays (same for the image name).
Edit
This is how I would implement the mechanism: an additional property that points to one of the three arrays in TableOneViewController:
#property (nonatomic, weak) NSArray* currentArray;
This pointer should point to the correct array, according to the tag of the sender that triggered tableSelection: :
// TableViewController
- (IBAction)tableSelection:(id)sender
{
TableOneViewController *TOVC = [self.storyboard instantiateViewControllerWithIdentifier:#"TableOneViewController"];
switch([sender tag]) {
case 1:
TOVC.currentArray= TOVC.numOne;
break;
case 2:
TOVC.currentArray= TOVC.numTwo;
break;
case 3:
TOVC.currentArray= TOVC.numThree;
break;
default:
return;
}
[self presentViewController:TOVC animated:YES completion:nil];
}
// TableOneViewController
- (IBAction)nextButton:(id)sender
{
int index = tableCounter++ % [self.currentArray count];
self.num.image = self.currentArray[index];
}
It doesn't work because
TableOneViewController *TOVC = [self.storyboard instantiateViewControllerWithIdentifier:#"TableOneViewController"];
creates a completely new instance of your TableOneViewController. I would use a singleton retaining the arrays and any other info I need as a single access point during the execution of my program.
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
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.