How to streamline my code - UIImage imageNamed: - iphone

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;

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"];
});
});

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.

frame by frame animation

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

iphone UIImage array memory leak

I had that question before, tried as answered, but still it does not work.
Before I allocated with:
imageArray_danceright = [[NSArray alloc] initWithObjects:
I came adviced to do like bellow, but now it crash immediatly after second anim is called:
//init areas:
imageArray_stand = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FrankieSingtRetime_0001" ofType:#"jpg"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FrankieSingtRetime_0002" ofType:#"jpg"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FrankiePeaceRetime_0003" ofType:#"jpg"]],nil];
imageArray_danceleft = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FrankiePeaceRetime_0001" ofType:#"jpg"]],
.. 40 images
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FrankiePeaceRetime_0040" ofType:#"jpg"]],nil];
imageArray_danceright = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FrankiePeaceRetime_0041" ofType:#"jpg"]],
.. 40 images
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FrankiePeaceRetime_0080" ofType:#"jpg"]],nil];
//start:
[myimageview setAnimationImages:imageArray_stand];
myimageview.animationDuration = 0.23;
myimageview.contentMode = UIViewContentModeBottomLeft;
myimageview.animationRepeatCount = 0.0;
myimageview.image = [myimageview.animationImages objectAtIndex:1];
[myimageview startAnimating];
// ------ in my 'touchesBegan" i have:
if ([touch view] == overlay_fuesserechts) {
[myimageview.animationImages release];
[myimageview setAnimationImages:imageArray_danceright];
myimageview.animationDuration = 2.0;
myimageview.contentMode = UIViewContentModeBottomLeft;
myimageview.animationRepeatCount = 0;
[myimageview startAnimating];
}
- (void)dealloc {
[imageArray_stand release];
imageArray_stand=nil;
[imageArray_danceright release];
imageArray_danceright=nil;
[imageArray_danceleft release];
imageArray_danceleft=nil;
super dealloc];
}
Its starts with the "stand" animation.. but when i fire touchesbegan it crashes immediatly at:
[myimageview setAnimationImages:imageArray_danceright];
and i have no idea. I tried so many things. Now I hope you have an idea.
thx
chris
Because you are autoreleasing your image arrays, by the time it gets to TouchesBegan they have already been released.
Who told you to use arrayWithObjects instead of alloc - initWithObjects ?
It sounds like your earlier version would have been more correct. What was the problem you were having with that?
You surely don't need the [myimageview.animationImages release]; at the beginning of touches began. You are creating autoreleased array objects and assigening them to the imageview which releases them.

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;