iphone imageview sequence animation - iphone

Hey, trying to put a simple png sequence animation into my app. I have the first frame in place in IB, and the graphanimation outlet connected to it.
There are 54 pngs in the sequence with names "Comp 1_0000.png" to "Comp 1_00053.png"
Here's my code.
-(void)viewDidLoad{
for (int i=0; i<53; i++) {
graphanimation.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"Comp 1_000%d.png",i]];
}
graphanimation.animationDuration = 1.00;
graphanimation.animationRepeatCount = 1;
[graphanimation startAnimating];
[self.view addSubview:graphanimation];
[super viewDidLoad];
}
I think something is wrong with the way I am referencing the image filenames with the i integer. Can someone help me sort this sucker out?
Thanks!

You can't pass a variable argument list and format arguments to [UIImage imageNamed:].
Try something like this perhaps?
...
NSMutableArray *array = [NSMutableArray arrayWithCapacity:54];
for (int i = 0; i < 54; ++i) {
NSString *name = [NSString stringWithFormat:#"Comp 1_000%d.png",i];
UIImage *image = [UIImage imageNamed:name];
[array addObject:image];
}
graphAnimation.animationImages = array;
...

Related

How to correctly preload image arrays for animation

I have several animations which, when triggered, have varying lengths of (unintentional) delays before they execute.
Inside viewDidLoad i have something like:
NSString *fileName;
myArray = [[NSMutableArray alloc] init];
for(int i = 1; i < 285; i++) {
fileName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"HD1.2 png sequence/HD1.2_%d", i] ofType:#"png"];
UIImage *image = [UIImage imageWithContentsOfFIle:fileName];
[humptyArray addObject:image];
//NSLog(#"Added object number %d: %#", i,regularImage);
}
falling.userInteractionEnabled = NO;
falling.animationImages = humptyArray;
falling.animationDuration = 6.3f;
falling.animationRepeatCount = 1;
I put in the NSLog so i could confirm that the array is populated with the images, which it is. When i want to trigger the animation i call [falling startAniamting]. Although the array has been preloaded with images there is still a delay between me triggering the animation and the animation executing.
What can i do so that there is no delay when i trigger the animation?
#autoreleasepool {
NSString *fileName;
humptyArray = [[NSMutableArray alloc] init];
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
for(int i = 1; i < 285; i++) {
fileName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"HD1.2 png sequence/HD1.2_%d", i] ofType:#"png"];
UIImage *image = [UIImage imageWithContentsOfFIle:fileName];
[humptyArray addObject:image];
//NSLog(#"Added object number %d: %#", i,regularImage);
}
falling.userInteractionEnabled = NO;
falling.animationImages = humptyArray;
falling.animationDuration = 6.3f;
falling.animationRepeatCount = 1;
dispatch_async(dispatch_get_main_queue(), ^{
[humptyArray release];
});
});
}
Taken from my own answer in this other [SO question][1]
[1]: http://stackoverflow.com/a/11364856/253008
You will find that people on SO suggest using animationImages, but it can eat up all your system memory and it is not fast. Please have a look at my answer to smooth-video-looping-in-ios, it is talking about looping video but the issue is the same. Please also take a look at my answer to iphone-smooth-transition-from-one-video-to-another. There are 2 example xcode projects available at these links that show an implementation that will start playing quickly and loop without any glitch all without eating up all your memory and causing an app crash.

how to animate a set of images?

how to animate a set of images, but I have a large set of images, 300 to be exact,
Background.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"001.jpg"],
[UIImage imageNamed:#"002.jpg"],
[UIImage imageNamed:#"003.jpg"],
[UIImage imageNamed:#"004.jpg"],
[UIImage imageNamed:#"005.jpg"],
[UIImage imageNamed:#"006.jpg"],
[UIImage imageNamed:#"007.jpg"],
[UIImage imageNamed:#"008.jpg"],
[UIImage imageNamed:#"009.jpg"],
[UIImage imageNamed:#"010.jpg"],
nil];
and I dont wanna go all the way up to 300 by typing, is there a solution for this?
Could I use a loop for this?
A loop might look like this
NSMutableArray *animationImages = [[NSMutableArray alloc] initWithCapacity:300];
for (int i = 1; i <= 300; i++) {
NSString *imageName = [NSString stringWithFormat:#"%03d.jpg", i];
[animationImages addObject:[UIImage imageNamed:imageName]];
}
Background.animationImages = animationImages;
The format specifier %03d means an integer padded with 0's to give a minimum field width of 3.
BUT
depending on the size of your images you may well run out of memory and have your app terminated.
Side note
Background is not a very good name for a variable on Objective-C. The convention is to start variable names with a lowercase letter and then use camel case after that.
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < 300; i++)
{
NSString *imgName = [NSString stringWithFormat:#"%03d.jpg", i];
UIImage *img = [UIImage imageNamed:imgName];
[array addObject:img];
}
Background.animationImages = array;
Use GIF images to play animation. Playing GIF files with OpenGLES is the good practice and I believe it is the best way to play animation.
Follow the Git Link: https://github.com/jamesu/glgif

I want to display a image in UIImagaView tat image names stored in array .so how to get string from array in iphone

Actually i want to display iamge in UIImageView ...tat image names are getting from my database..so how to display image from getting string in an array in iphone
use this methods
UIImageView *imageView = [UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
imageView.animationImages = animationImages ;
imageView.animationRepeatCount = 2;
imageView.animationDuration= 4.0;
[imageView startAnimating];
animationImages is an Array containig UIImage Objects.
NSString *imageNameStr = [NSString stringWithFormat:#"%#",[imageNameArray objectAtIndex:index]];
UIImageView *myImage;
myImage=[[UIImageView alloc]initWithFrame:CGRectMake(00.0, 00.0, 320.0, 480.0)];
myImage.backgroundColor=[UIColor clearColor];
myImage.image=[UIImage imageNamed:imageNameStr];
[self.view addSubview:myImage];
hope it Help To you
You can use the following code.
NSString *str = [NSString stringWithFormat:#"%#",[arr_ArrayName objectAtIndex:index]];
NSString *path = [[ NSBundle mainBundle] pathForResource:str ofType:#"png"];
UIImage *temp = [UIImage imageWithContentsOfFile:path];
yourImageView.image = temp;
Pass the index value as an integer which image from array you want to display.
Edit:
Declare one integer temp in your view will appear. Set it's value to 0. temp = 0;
Then, inside your button action please write the below mentioned code:
-(void)buttonClick
{
NSString *selectedItem =[NSString stringWithFormat:#"%#",[colors objectAtIndex:temp]];
NSString *wrdstr = [[ NSBundle mainBundle] pathForResource:selectedItem ofType:#"png"];
UIImage *temp = [UIImage imageWithContentsOfFile:wrdstr];
imgshow.image = temp;
if(temp <= [colors count])
temp = temp + 1;
}

Error: Variable-sized object may not be initialized. But why?

enter code hereint quantity = [array count];
int i;
for (i=0; i<quantity; i++)
{
NSString *imageName = [NSString stringWithFormat:#"Car_%#.jpg", [[array objectAtIndex:i] objectForKey:#"CarName"]] ];
UIImage *img[i] = [UIImage imageNamed:imageName];
UIImageView *imgView[i] = [[UIImageView alloc] initWithImage:img[i]];
imgView[i].frame = CGRectMake(i*kWidth, 0, kWidth, kHeight);
[scrollView addSubview:imgView[i]];
[imgView[i] release];
}`enter code here`
Error: Variable-sized object may not be initialized. But why?
UIImage *img[i] = [UIImage imageNamed:imageName];
This declares a C-style array of size i and attempts to initialise it with an instance of UIImage. That doesn't make sense. What are you trying to do? Where is the rest of your code?
Edit:
Okay, I think I see what you are doing. Just get rid of all the places you have [i]. Inside the loop, you are only dealing with one item at a time, and even if you weren't, that's not how you use arrays.
You may want to try this:
int i;
for (i=0; i<quantity; i++)
{
NSString *imageName = [NSString stringWithFormat:#"Car_%#.jpg", [[array objectAtIndex:i] objectForKey:#"CarName"]] ];
UIImage *img = [UIImage imageNamed:imageName];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
imgView.frame = CGRectMake(i*kWidth, 0, kWidth, kHeight);
[scrollView addSubview:imgView];
[imgView release];
}
You don't need to use img[i] in order to populate a scrollview with UIImageView.

How to construct IBOutlet names in code in order to change them?

Let's say I've got several UILabels which have been set up in IB and connected to IBOulets in code (label1, label2, label3, label4)
How would I create these variable names within code, so that I could change the text of each one in a loop, where the labels are taken from a NSArray.
Here's the pseudo-code:
labelArray = [NSArray arrayWithObjects:#"this", #"array", #"has", #"a", #"random", #"amount", #"of", #"items", nil];
for (int i = 0; i < [labelArray count]; i++)
{
// labelx is the constructed name of the IBOutlet
lablex.text = [labelArray objectAtIndex:i];
}
How do I construct 'labelx' above? Could this be done using Blocks?
You would have to initialize the array at some place, using
labelArray = [NSArray arrayWithObjects:#"this", #"array", #"has", nil];
uiLabelArray = [NSArray arrayWithObjects:label1,label2,label3,nil];
then
for (int i = 0; i < [uiLabelArray count]; i++)
{
[uiLabelArray objectAtIndex:i].text = [labelArray objectAtIndex:i];
}
You can use key value coding (KVC). It would look something like:
[[self valueForKey:[NSString stringWithFormat:#"label%d", i]] setText:[labelArray objectAtIndex:i]];
More info can be found here