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.
Related
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];
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.
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;
This animates the UIImageView in the impactdrawarray:
if ( ((impact *) [impactarray objectAtIndex:iv]).animframe == 70){
((UIImageView *) [impactdrawarray objectAtIndex:iv]).image =
[UIImage imageWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:#"explosionA71"
ofType:#"png"]];
}
if ( ((impact *) [impactarray objectAtIndex:iv]).animframe == 71){
((UIImageView *) [impactdrawarray objectAtIndex:iv]).image =
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:#"explosionA72"
ofType:#"png"]];
}
Nothing appears with this:
NSString *myString2 =
[NSString stringWithFormat:#"A%d",
((impact *) [impactarray objectAtIndex:iv]).animframe;
((UIImageView *) [impactdrawarray objectAtIndex:iv]).image =
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:(#"explosion%d", myString2)
ofType:#"png"]];
My reference for using NSString (example)
NSString *myString23 = [NSString stringWithFormat:#"$%d", money];
moneylabel.text = myString23;
How can I use a variable in the file name? Is there a way to load an image in a resource folder by index? something like imageByIndex:currentanimationframe? I didn't see anything in the UIImage documentation.
Response to comments:
NSString *myString2 = [NSString stringWithFormat:#"A%d", ((impact *) [impactarray objectAtIndex:iv]).animframe];
((UIImageView *) [impactdrawarray objectAtIndex:iv]).image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"explosion%d", myString2] ofType:#"png"]];
and
NSString *imagename = [NSString stringWithFormat:#"A%d", ((impact *) [impactarray objectAtIndex:iv]).animframe];
UIImage *myImage = [UIImage imageNamed:imagename];
((UIImageView *) [impactdrawarray objectAtIndex:iv]).image = myImage;
Both compile and run, but the image does not show.
Use +stringWithFormat:, just as in your example:
[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"explosion%d", myString2] ofType:#"png"];
NSString* imageName = [self dynamicallyCalculateImageNameUsingCrazyMath];
UIImage* myImage = [UIImage imageNamed:imageName];
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;