frame by frame animation - iphone

i wanna knw an alternate to this animation code..
-(void)movetree{
//[tree1 release];
treeMove = YES;
tree1.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"moving trees1.png"],
[UIImage imageNamed:#"moving trees2.png"],
[UIImage imageNamed:#"moving trees3.png"],
[UIImage imageNamed:#"moving trees4.png"],
[UIImage imageNamed:#"moving trees5.png"],
[UIImage imageNamed:#"moving trees6.png"],
[UIImage imageNamed:#"moving trees7.png"],
[UIImage imageNamed:#"moving trees8.png"],
[UIImage imageNamed:#"moving trees9.png"],
[UIImage imageNamed:#"moving trees10.png"],
[UIImage imageNamed:#"moving trees11.png"],
[UIImage imageNamed:#"moving trees12.png"],
[UIImage imageNamed:#"moving trees13.png"],
[UIImage imageNamed:#"moving trees14.png"],
[UIImage imageNamed:#"moving trees15.png"],
[UIImage imageNamed:#"moving trees16.png"],
[UIImage imageNamed:#"moving trees17.png"],
[UIImage imageNamed:#"moving trees18.png"],
[UIImage imageNamed:#"moving trees19.png"],
[UIImage imageNamed:#"moving trees20.png"],
[UIImage imageNamed:#"moving trees21.png"],
[UIImage imageNamed:#"moving trees22.png"],
[UIImage imageNamed:#"moving trees23.png"],
[UIImage imageNamed:#"moving trees24.png"],
[UIImage imageNamed:#"moving trees25.png"],
[UIImage imageNamed:#"moving trees26.png"],
[UIImage imageNamed:#"moving trees27.png"],
[UIImage imageNamed:#"moving trees28.png"],
[UIImage imageNamed:#"moving trees29.png"],
[UIImage imageNamed:#"moving trees30.png"],
[UIImage imageNamed:#"moving trees31.png"],
[UIImage imageNamed:#"moving trees32.png"],
[UIImage imageNamed:#"moving trees33.png"],
[UIImage imageNamed:#"moving trees34.png"],
[UIImage imageNamed:#"moving trees35.png"],
[UIImage imageNamed:#"moving trees36.png"],
[UIImage imageNamed:#"moving trees37.png"],
[UIImage imageNamed:#"moving trees38.png"],
[UIImage imageNamed:#"moving trees39.png"],
[UIImage imageNamed:#"moving trees40.png"],
[UIImage imageNamed:#"moving trees41.png"],
[UIImage imageNamed:#"moving trees42.png"],
[UIImage imageNamed:#"moving trees43.png"],
[UIImage imageNamed:#"moving trees44.png"],
[UIImage imageNamed:#"moving trees45.png"],
[UIImage imageNamed:#"moving trees46.png"],
[UIImage imageNamed:#"moving trees47.png"],
[UIImage imageNamed:#"moving trees48.png"],
[UIImage imageNamed:#"moving trees49.png"],
[UIImage imageNamed:#"moving trees50.png"],
[UIImage imageNamed:#"moving trees51.png"],
[UIImage imageNamed:#"moving trees52.png"],
[UIImage imageNamed:#"moving trees53.png"],
[UIImage imageNamed:#"moving trees54.png"],
[UIImage imageNamed:#"moving trees55.png"],
[UIImage imageNamed:#"moving trees56.png"],
[UIImage imageNamed:#"moving trees57.png"],
[UIImage imageNamed:#"moving trees58.png"],
[UIImage imageNamed:#"moving trees59.png"],
[UIImage imageNamed:#"moving trees60.png"],nil];
// all frames will execute in 1.75 seconds
tree1.animationDuration =1.75;
// repeat the annimation forever
tree1.animationRepeatCount = 0;
// start animating
[tree1 startAnimating];
// add the animation view to the main window
//[bgview addSubview:campFireView];
[tree1 release];
player.numberOfLoops = -1;
[player play];
}
Is there any other way to show animation using frames?? this method seems to be heavier and it crashes alot..

Try using PNG Animation method by Moses DeJong. From his words:
This example implements an animation
oriented view controller that simply
waits to read the PNG image data for a
frame until it is needed. Instead of
alllocating many megabytes, this class
run in about a half a meg of memory
with about a 5-10% CPU utilization on
a 2nd gen iPhone.
I believe it basically uses UIImageView to cache up AVAudioPlayer.

there is a call back for the animation, something like animation finished, [self performSelector:#selector(stopAnimation) withObject:nil afterDelay:3]; cant remember offhand - But the usual way is, once the animation is done this method is called and then u can release. The usual EXC_BAD_ACCESS could be one or more of your images do not exist, double check all the image names and verify if they exist in your resource folder. BUt that's the only as far as i know for animation images.
helper link http://iosdevelopertips.com/graphics/animated-gif-animated-images-iphone-style.html

Related

How can i show user avatar image using xmpp framework in objective c?

I am creating a chat application for iPhone using XMPPFramework. I want to display user list in UITableView with user name and photo, and for this I am using
NSData *photoData = [[[self appDelegate] xmppvCardAvatarModule] photoDataForJID:user.jid];
if (photoData != nil)
cell.imageView.image = [UIImage imageWithData:photoData];
else
cell.imageView.image = [UIImage imageNamed:#"blank_image~iPhone.png"];
but it returns nil always.
Please help..
NSData *photoData = UIImagePNGRepresentation([[[self appDelegate] xmppvCardAvatarModule] photoDataForJID:user.jid]);
if (photoData)
cell.imageView.image = [UIImage imageWithData:photoData];
else
cell.imageView.image = [UIImage imageNamed:#"blank_image~iPhone.png"];
This code is working fine.
photoDataForJID method returns NSData.
NSData *avtarData = [[[self appDelegate] xmppvCardAvatarModule] photoDataForJID:user.jid];
if (avtarData)
cell.imageView.image = [UIImage imageWithData:photoData];
else
cell.imageView.image = [UIImage imageNamed:#"blank_image~iPhone.png"];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSData *imgData = UIImagePNGRepresentation([[[self appDelegate] xmppvCardAvatarModule] photoDataForJID:user.jid]);
dispatch_async(dispatch_get_main_queue(), ^{
if (imgData)
cell.imageView.image = [UIImage imageWithData:imgData];
else
cell.imageView.image = [UIImage imageNamed:#"default_user.png"];
});
});

How to programmatically set the background on a UICustomButton?

I know this question has been asked and answered in other forums, but I can't seem to duplicate what they did and get it to work. I am trying to set the background to a button using a picture that I download from a server. Everything works until I try to make a NSData object with the contents of a URL. When I try and print the Data to the screen it comes back Null. What am I doing wrong?
myImageURLString = [[NSMutableString alloc] initWithString:#"http://destinationnexus.com"];
keyName = [businessButtonTagArray objectAtIndex:i];
arrayFromBusinessDictionary = [businessDictionary objectForKey:keyName];
stringToAddToMyImageURLString = [arrayFromBusinessDictionary objectAtIndex:1];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:i];
button.frame = CGRectMake(xOffSet+(countForX*xMult), yOffset +(countForY*yMult), 215, 155);
[myImageURLString appendString:stringToAddToMyImageURLString];
NSLog(#"Appended URL String: = %#", myImageURLString);
NSURL *myImageURL = [NSURL URLWithString:#"http://destinationnexus.com/pictures/directory/ColdWater-Inn-in-Tuscumbia-Alabama-35674-8729//.jpg"];
NSLog(#"NSURL = %#", myImageURL);
//THIS IS WHERE I SEEM TO HAVE A PROBLEM.....
NSData *imageData = [[NSData alloc] initWithContentsOfURL:myImageURL];
UIImage *image = [[UIImage alloc] initWithData:imageData] ;
NSLog(#"Image Data = %#", imageData);
//UIImage *myImage = [UIImage imageWithData:imageData] ;
[button setBackgroundImage:image forState:UIControlStateNormal];
When I run the program this is what it outputs in the Log:
Appended URL String: = http://destinationnexus.com/pictures/directory/Auburn-University-Hotel-and-Center-in-Auburn-Alabama-36830-5429.jpg
NSURL = http://destinationnexus.com/pictures/directory/ColdWater-Inn-in-Tuscumbia-Alabama-35674-8729.jpg
Image Data = (null)
You should correct your myImageURL, by deleting 2 slashes at the end of URL.
The code goes something like this.
NSURL *myImageURL = [NSURL URLWithString:#"http://destinationnexus.com/pictures/directory/ColdWater-Inn-in-Tuscumbia-Alabama-35674-8729.jpg"];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:myImageURL];
UIImage *image = [[UIImage alloc] initWithData:imageData] ;
[button setBackgroundImage:image forState:UIControlStateNormal];
[image release];
[imageData release];

UIImageView doesn't animate

I have the following code inside my app, but it doesn't animate the UIImageView, can anyone tell me why?
UITableViewCell* cell = [selectedTable cellForRowAtIndexPath:selectedPath];
NSArray* myImageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:#"boxBeingChecked117x11" ofType:#"png"]],
[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:#"boxBeingChecked217x11" ofType:#"png"]],
[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:#"boxBeingChecked317x11" ofType:#"png"]],
[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:#"boxBeingChecked417x11" ofType:#"png"]],
[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:#"boxBeingChecked517x11" ofType:#"png"]],
[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:#"boxBeingChecked617x11" ofType:#"png"]],
nil];
cell.imageView.animationImages = myImageArray;
cell.imageView.animationDuration = 4.20;
cell.imageView.animationRepeatCount = 1;
[cell.imageView startAnimating];
[NSTimer scheduledTimerWithTimeInterval:4.0
target:self
selector:#selector(methodToInvokeAfterAnimationIsDone)
userInfo:nil
repeats:NO];
You should use this:
NSArray* myImageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"boxBeingChecked117x11.png"]],
[UIImage imageNamed:#"boxBeingChecked217x11.png"]],
[UIImage imageNamed:#"boxBeingChecked317x11.png"]],
[UIImage imageNamed:#"boxBeingChecked417x11.png"]],
[UIImage imageNamed:#"boxBeingChecked517x11.png"]],
[UIImage imageNamed:#"boxBeingChecked617x11.png"]],
nil];
You need to give name of the file, not the path, in imageNamed: method.
Make sure that the imageView's highlighted property is set to NO, otherwise the code you have above will not work. If you want to animate highlighted images, use highlightedAnimationImages.

clear image array on button click

i have an array
imageArray = [[NSArray alloc] initWithObjects:[UIImage imageNamed:#"1.png"],
[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:#"11.png"],
[UIImage imageNamed:#"12.png"],
nil];
i want to clear this array on button click.
these are series of images on screen.
and if i press button clearing the image should clear all the images.
How can i do it?
regards
shishir
[imageArray release];
imageArray = nil;

How to streamline my code - UIImage imageNamed:

More iPhone stuff...
Does anyone know how I can trim this code down. So I don't have to write every single image.
Thanks a lot
bMon01.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"mon01_0001.png"],
[UIImage imageNamed:#"mon01_0002.png"],
[UIImage imageNamed:#"mon01_0003.png"],
[UIImage imageNamed:#"mon01_0004.png"],
[UIImage imageNamed:#"mon01_0005.png"],
[UIImage imageNamed:#"mon01_0006.png"],
[UIImage imageNamed:#"mon01_0007.png"],
[UIImage imageNamed:#"mon01_0008.png"],
[UIImage imageNamed:#"mon01_0009.png"],
[UIImage imageNamed:#"mon01_0010.png"],
[UIImage imageNamed:#"mon01_0011.png"],
[UIImage imageNamed:#"mon01_0012.png"],
[UIImage imageNamed:#"mon01_0013.png"],
[UIImage imageNamed:#"mon01_0014.png"],
[UIImage imageNamed:#"mon01_0015.png"],
[UIImage imageNamed:#"mon01_0016.png"],
[UIImage imageNamed:#"mon01_0017.png"],
[UIImage imageNamed:#"mon01_0018.png"],
[UIImage imageNamed:#"mon01_0019.png"],
[UIImage imageNamed:#"mon01_0020.png"],
[UIImage imageNamed:#"mon01_0021.png"],
[UIImage imageNamed:#"mon01_0022.png"],
[UIImage imageNamed:#"mon01_0023.png"],
[UIImage imageNamed:#"mon01_0024.png"],
[UIImage imageNamed:#"mon01_0025.png"], nil];
Will this help:
NSMutableArray *array = [NSMutableArray array];
for (int i = 1; i <= 25; i++)
[array addObject:[UIImage imageNamed:[NSString stringWithFormat:#"mon01_%04d.png",i]]];
bMon01.animationImages = array;