UIImageView animation after action? - iphone

I have a problem setting up an animation. The first imageview starts animating and when playsound1 is called then starts the second animation. Everything works fine but right now when the second animation stops there is no animation going on. So what I want to do is after the second animation stops - the first to start all over-- then again when method is called to play the second animation . Any hints?
Here you can see a part of the code as it is right now:
- (void)loadtest1 {
NSArray *imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"test1.png"],
[UIImage imageNamed:#"test2.png"],
[UIImage imageNamed:#"test3.png"],
[UIImage imageNamed:#"test4.png"],
nil];
test1.animationImages = imageArray;
test1.animationRepeatCount = 0;
test1.animationDuration = 1;
[imageArray release];
[test1 startAnimating];
}
- (void)loadtest2 {
NSArray *imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"test4.png"],
[UIImage imageNamed:#"test5.png"],
[UIImage imageNamed:#"test6.png"],
[UIImage imageNamed:#"test7.png"],
nil];
test2.animationImages = imageArray;
test2.animationRepeatCount = -1;
test2.animationDuration = 1;
[imageArray release];
[test2 startAnimating];
}
- (IBAction)playsound1 {
NSString *path = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
test1.hidden = 0;
test2.hidden = 1;
[test1 startAnimating];
test2.center = test1.center;
}

If I understand you correctly, what you are trying to do is re-starting your first animation when the second one finishes
The correct way to handle this is through a CABasicAnimation, which allows you to specify a delegate whose – animationDidStop:finished: method is called when the animation is done.
If you do not want this way, a sort of workaround is the following: in loadtest2, after you start the animation, schedule a method for execution with a delay:
[self performSelector:#selector(loadTest1) withObject:nil afterDelay:1.0];
this will (more or less) work satisfactorily because you know how long your animation will last. So, when the animation should be ended, loadTest1 is executed and the first animation starts again.

Related

App getting crash in background image loading iphone

I have scroll view with 60 UIImageView's. Images displaying in these imageviews are from url and url's i get from the webservice. When user scrolls to bottom I call the webservice and get new 60 urls. After getting the url i am utilizing same UIImageView's to display images. Following is my code for displaying images.
for (UIView *viewSel in [scrlView subviews]) {
NSString *strImgUrl = [arrImgURL valueForKey:#"url"];
if ([viewSel isKindOfClass:[UIImageView class]]) {
UIImageView *imgView = (UIImageView *)viewSel;
[imgView setImage:[UIImage imageNamed:#"loading432x520.png"]];
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr addObject:[NSString stringWithFormat:#"%#",strImgUrl]];
[arr addObject:imgView];
[self performSelectorInBackground:#selector(loadImageInBackground:) withObject:arr];
[arr release];
}
}
- (void) loadImageInBackground:(NSMutableArray *)arr {
NSLog(#"loadImage");
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[arr objectAtIndex:0]]];
UIImage *img = [[UIImage alloc] initWithData:imgData];
if (img != nil) {
[arr addObject:img];
}
else{
[arr addObject:[UIImage imageNamed:#"no-image432x520.png"]];
}
[img release];
[self performSelectorOnMainThread:#selector(assignImageToImageView:) withObject:arr waitUntilDone:YES];
[pool release];
}
- (void) assignImageToImageView:(NSMutableArray *)arr{
NSLog(#"assignImage");
UIImageView *imgView = [arr objectAtIndex:1];
imgView.image = [arr objectAtIndex:2];
}
The code works perfect for first time. But when i get new urls it is working some time or getting crash. I don't know why it is getting crash. I want to stop that crash. If you are not getting to my question then let me know. Please help me for this. Thanks in advance. Valid answer will be appreciated.
Thank you all to give your help full comments to my question. I got the error. I am adding CALayer to uiimageview when allocating memory to uiimageview. I remove that CALayer code and it work perfectly. Nikita's comment help me to find my error.

Can't tweet an image

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
forTweet = TRUE;
switch(buttonIndex)
{
case 0:
{
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
// Set the initial tweet text. See the framework for additional properties that can be set.
NSMutableString * twitterString = [displayString mutableCopy];
[twitterString appendString: #" #LoviOtvet"];
[tweetViewController setInitialText: twitterString];
UIImage * imageAnswer = [self getImageFromURL: [self strToUrlConverter]];
UIImage * imageLogo = [[UIImage alloc] initWithContentsOfFile: #"image.png"];
[tweetViewController addImage: [self mergeImage:imageAnswer withImage:imageLogo strength:1]];
break;
}
I added . No errors, but not working.
Facebook and saving to device working. Tweeting not. Why not?
Your problem is this line:
UIImage * imageLogo = [[UIImage alloc] initWithContentsOfFile: #"image.png"];
For this method, you would need to need to provide a file path, not a name. You have two choices:
UIImage * imageLogo = [UIImage imageNamed:]
or (for a file in the bundle):
NSString *path = [[NSBundle mainBundle] pathForResource:#"image" ofType:#"png"];
UIImage * imageLogo = [[UIImage alloc] initWithContentsOfFile:path];

UIButtons event Touch up Inside not work properly?

My question is little abmigious ,thats why I posted all my code,so I request to every one please test all this code before giving me the Answer.Thanx
In my application i create all UIButtons programmatically and then save all these UIButtons in NSMutableArray. Here is my code:-
-(void)button:(id)sender
{
int btnn = 0;
int spacex = 152;
int spacey=20;
int k=0;
saveBtn = [[NSMutableArray alloc] init];
for (int i=0; i<48; i++)
{
if (btnn>6)
{
spacey=spacey+25;
spacex = 152;
btnn = 0;
}
else
{
btnn++ ;
k++;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(spacex, spacey, 25.0, 25.0);
int idx;
idx = arc4random()%[arr count];
NSString* titre1 = [arr objectAtIndex:idx];
[btn setTitle: titre1 forState: UIControlStateNormal];
[btn setTitleColor: [UIColor yellowColor] forState: UIControlStateNormal];
[btn.titleLabel setFont:[UIFont fontWithName:#"TimesNewRomanPS-BoldMT" size:22.0]];
spacex = spacex + 25;
btn.tag=k;
[btn addTarget:self action:#selector(aMethod:)forControlEvents:UIControlEventTouchUpInside];
[saveBtn addObject:btn];
[self.view addSubview:btn];
}
}
}
Now in (aMethod:) I add click sound on Each UIButton TouchUpInside event. Here is my code.
- (void)aMethod:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"button-17" ofType:#"wav"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
}
Every thing work fine in start of my application , but continiously clicking the UIButtons approximatilly after 100 clicks i hear no sound of click and when i click on Next UIButtons touchup inside event its crash.Can any body help me in this issue.Thanx
app due to uncaught exception NSInternalInconsistencyException, reason: Could not load NIB in bundle: NSBundle </Users/moon/Library/Application Support/iPhone Simulator/4.1/Applications/3E14E5D6-4D1B-4DAC-A2B9-07667E99C399/soundtesting.app‌​> (loaded) with name secondclass
Ah, yes. What we have here is a failure to understand log messages. Your UIButton, which I can only assume pushes a new view onto the stack, is referencing a NIB that simply doesn't exist. It's a naming issue, not a button issue. Most likely, I would check for any calls to -initWithNibName: and see if your NIB really is named "secondclass.". Remember, iOS file names are CaSe SeNsITive.
//.h
...
#property (nonatomic, retain) AVAudioPlayer * player;
//.m
-(void)viewDidLoad
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"button-17" ofType:#"wav"];
player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
player.delegate=self;
//in MRC, use [path release];
}
-(void)someMethod:(id)sender
{
[player play];
}
The reason might me memory leak for playing same audio these many times it may fill memory use this code for playing audio
- (id)initWithNibName:(NSString*)nibName bundle:(NSBundle*)nibBundleOrNil
{
if (self = [super initWithNibName:nibName bundle:nibBundleOrNil]) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"button-17" ofType:#"wav"];
theAudio = [AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath] error:NULL];
}
return self;
}
- (IBAction)playSound
{
[theAudio play];
}
- (void)dealloc
{
[theAudio release];
[super dealloc];
}
You have a memory issue. When you do :
- (void)aMethod:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"button-17" ofType:#"wav"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
}
1/ path is never released
2/ theAudio is never release
So you need to release theAudio in delegate method :
– audioPlayerDidFinishPlaying:successfully:
And you need to release path like this :
- (void)aMethod:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"button-17" ofType:#"wav"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
[path release];
}
I think your problem should be solved.
I test your code . Changes in your aMethod following code. Though this I can get tag value.
- (void)aMethod:(id)sender
{
UIButton *btn=(UIButton *)sender;
NSLog(#"\n\n\nselectedbutton tag === %d \n\n\n",btn.tag );
NSString *path = [[NSBundle mainBundle] pathForResource:#"button-17" ofType:#"wav"];
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
[audioPlayer play];
}

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.

code to play sound programmatically using a button on the iphone

I am trying to figure out how to hook up a button to play a sound programmatically without using IB. I have the code to play the sound, but have no way of hooking the button up to play the sound? any help?
here is my code that I'm using:
- (void)playSound
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"boing_1" ofType:#"wav"];
AVAudioPlayer* myAudio = [[AVAudioPlayer alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:path error:NULL]];
myAudio.delegate = self;
myAudio.volume = 2.0;
myAudio.numberOfLoops = 1;
[myAudio play];
}
[button addTarget:self action:#selector(playSound) forControlEvents:UIControlEventTouchUpInside];
UIButton inherits its target/action methods from UIControl.
To hook up the button, make your playSound method the handler for the button's UIControlEventTouchUpInside event. Assuming this is in a view controller, you might want to put this in the viewDidLoad method:
[button addTarget:self action:#selector(playSound) forControlEvents:UIControlEventTouchUpInside];
FYI, you're leaking memory because you're alloc-ing an object but never release-ing it.
You should create a new AVAudioPlayer member for the class to avoid this.
#interface MyViewController : ...
{
...
AVAudioPlayer* myAudio;
...
}
- (void)playSound
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"boing_1" ofType:#"wav"];
[myAudio release];
myAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path error:NULL]];
myAudio.delegate = self;
myAudio.volume = 2.0;
myAudio.numberOfLoops = 1;
[myAudio play];
}
Don't forget to put [myAudio release] in your dealloc method.
(I did this without declaring myAudio as a #property, but that's not strictly necessary)