UIImageView animation has long delay on device - iphone

I create a 5-frame animation on UImageView and each image is created by [UIImage imageWithContentOfFile:xxx],
however, the animation on simulator is working well but not on device, it usually has ~1 sec delay though the animation duration is 1sec.
This is my code.
NSArray *animations = [[NSBundle mainBundle] pathsForResourcesOfType:#"png" inDirectory:#"ShakeAnimation"];
UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageWithContentsOfFile:[animations objectAtIndex:0]]];
imgView.tag = 10;
NSMutableArray *animationImgs = [[NSMutableArray alloc] init];
for( NSString *path in animations )
{
[animationImgs addObject:[UIImage imageWithContentsOfFile:path]];
}
[imgView setAnimationRepeatCount:2];
[imgView setAnimationImages:animationImgs];
[imgView setAnimationDuration:1];
[animationImgs release];
and when the view controller recieved shack event, it calls [imgView startAnimation];
Is anyway to preload the images involved in the animation? thanks.

I figured it out!
If you want to preload the UIImageView animation images, just call [imgView startAnimating] after initialization. It seemed to work for me.

Related

Three20 TTTableItem elements background colour changed to white in IOS5

I have a TTTableViewController with a background image in which I insert items of type TTTableLink. Running the app if I drag the cells out of the screen by scrolling up and down for example, when they enter the screen again their background colour is changed to white. I have noticed this problem only in iOS5.
I don't really get when the background is changed, any ideas of how to preserve clear background for the table cells
Here is my code:
-(void) loadView {
[super loadView];
UIImage *backgroundImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"imageName" ofType:#"png"]];
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
backgroundView.frame = CGRectMake(0, 0, 320, 416);
[self.view addSubview:backgroundView];
[self.view sendSubviewToBack:backgroundView];
[backgroundView release];
self.tableView.backgroundColor = [UIColor clearColor];
}
-(void) createModel {
TTListDataSource* listDataSource= [[[TTListDataSource alloc] init] autorelease];
TTTableLink *item = [TTTableLink itemWithText:itemName URL:targetURL];
[listDataSource.items addObject:item];
}
There's a pending pull request on three20 which fixes this issue. see https://github.com/facebook/three20/pull/689. post a comment on it, so the repo owner will merge it. I think he forgot about this project :-)
or you can either fix it by changing that line in your source code

How to remove overlapping uiimageview?

I tried to change the image in the imageview but I can't seem to do that.
My code shown below creates a new subview for each image to display. However, the images just overlap and shows the overlapped images at the end which is not supposed to be.
.m:
for (int i = 0; i < [imageArr count]; i++) {
NSArray *imageNameArr = [[imageArr objectAtIndex:i] componentsSeparatedByString:#"."];
check=line;
NSString *msg = [textView.text stringByAppendingString:[NSString stringWithFormat:#"Opening image: %#\n",[imageArr objectAtIndex:i]]];
[textView performSelectorOnMainThread:#selector(setText:) withObject:msg waitUntilDone:NO];
line=line+1;
NSString *imagePath = [NSString stringWithFormat:#"%#/%#",jName,[imageArr objectAtIndex:i]];
NSString *imageFile = [NSHomeDirectory() stringByAppendingPathComponent:imagePath];
NSLog(#"------------------------------");
NSLog(#"ImageFile: %#\n",imageFile);
NSLog(#"Interger value: %i\n",i);
UIImage *myImage = [UIImage imageWithContentsOfFile:imageFile];
UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[scrollView addSubview:imageView];
[imageView release];
.....
.....
}
How to show the images individually, without overlapping?
put this line out of for loop:
UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
[scrollView addSubview:imageView];
and use it like:
UIImageView *imageView = [[UIImageView alloc] init];
[scrollView addSubview:imageView];
and use:
[imageView setImage:[UIImage imageWithContentsOfFile:fullImgNm]];
Inside for loop.
You create the imageview out of loop and change the source(Image) for that imageview inside the loop.
You do this before loop starts,
UIImageView *imageView = [[UIImageView alloc] init];
[scrollView addSubview:imageView];
and add the image like this inside the loop,
[imageView setImage:[UIImage imageWithContentsOfFile:imageFile]];

UIImageView animation is not displayed on view

In my Iphone App, I used the following code for the animation of those 3 images in viewDidLoad() method and I use left transform to load this ViewController. But these imageView is not displayed on the view.
NSArray *newArray = [[NSArray alloc]initWithObjects:[UIImage imageNamed:#"search1.png"],
[UIImage imageNamed:#"search2.png"],
[UIImage imageNamed:#"seach3.png"],
nil];
UIImageView *imageView = [UIImageView alloc];
[imageView initWithFrame:CGRectMake(240, 60, 60, 30)];
imageView.animationImages = newArray;
imageView.animationDuration = 0.25;
imageView.animationRepeatCount = 3;
[self.view addSubview:imageView];
Animation won't start until you tell it to start. You will need to add a command to startAnimating. I also cleaned up your alloc/init and I added a command to make the image view still keep showing when it is not animating. Setting an animation will not make the image appear. The image property is for the still image and the animationImages property is for the animating images of a UIImageView.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 60, 60, 30)];
imageView.image = [UIImage imageNamed:#"search3.png"]; //This will be the image that is visible when it is NOT animating.
imageView.animationImages = newArray;
imageView.animationDuration = 0.25;
imageView.animationRepeatCount = 3;
[imageView startAnimating]; //This will make the animation start
[self.view addSubview:imageView];
Finally, your third image is missing an r. I think you want #"search.png" and not #"seach.png"

Why won't my UIImageView appear as a subview?

UIImage *image = [[UIImage alloc] initWithContentsOfFile:#"popup.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
[image release];
[imageView release];
The code sits within a UIViewcontroller object. It compiles fine but, when run, the subview does not appear to be added.
I'm tired.
initWithContentsOfFile requires the complete file path, not just the file name.
Replace the line where you initialize UIImage with the following:
NSBundle *bundle = [NSBundle mainBundle];
UIImage *image = [[UIImage alloc] initWithContentsOfFile: [bundle pathForResource:#"popup" ofType:#"png"]];
A simpler way will be to use the imageNamed: method of the UIImage class
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"popup.png"]];
And you dont have to worry about the path-to-file details. Just that the initWithImage: method caches the image. Depending upon your application might be good or bad idea.
Hope this helps!
Try to use:
UIImage *image = [UIImage imageNamed:#"popup.png"];
and, off course, get rid of the [image release]; because in this solution the image is autoreleased.
Notice that this solution won't be good if you have more than one image with this name (in different folders/groups)...
your imageView needs a frame.
imageView.frame = CGRectMake(0,0,320,480);
I know this question is old now, but some users can still benefit from an answer.
Like others have said, try using [UIImage imagedNamed:] instead of initWithContentsOfFile.
And the reason it is not showing up on screen is you did not set a location for the image. You can fix this by either setting a frame for the image view or setting its center at a single point. To place the image view in the center of the screen:
UIImage *image = [UIImage imageNamed:#"image.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.center = [self.view center];
// or if you want to set a frame instead
// CGRect imageFrame = CGRectMake(x,y,width,height);
// [imageView setFrame:imageFrame];
[self.view addSubView:imageView];
Enjoy :)

Am trying to add an animation to a class that has extended UIView, but it is not adding it at all!

This is the strangest error i have come across.
Am trying to add a countdown animation onto a Class that extends UIView. The code is shown below:
NSArray *myImages = [[NSArray alloc] initWithObjects: [UIImage imageNamed:#"3.png"], [UIImage imageNamed:#"2.png"], [UIImage imageNamed:#"1.png"], [UIImage imageNamed:#"Go.png"], nil];
UIImageView *myAnimatedView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 80, 80)];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = 3;
myAnimatedView.animationRepeatCount = 0;
[self addSubview:myAnimatedView];
[myAnimatedView startAnimating];
It is just not appearing on the screen. Any Ideas?
Have you made sure those images are added to your project?
Can you add them as a flat image (non-animated) -- as in:
UIImageView *myAnimatedView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"3.png"]];
[self addSubview:myAnimatedView];
(just to make sure that works first)?
If, instead of extending UIView, you extended UIImageView, would that make a difference?
Try switching the last 2 lines. From:
[self addSubview:myAnimatedView];
[myAnimatedView startAnimating];
To:
[myAnimatedView startAnimating];
[self addSubview:myAnimatedView];