UIImageView animating pictures - xcode

How do I cycle the images in an UIImageView?
Say I have three images, 1.jpg, 2.jpg, 3.jpg. How do I get the pictures to change ad infinitum until the user presses something...

This describes it pretty well: [Link]
// create the view that will execute our animation
UIImageView* campFireView = [[UIImageView alloc] initWithFrame:self.view.frame];
// load all the frames of our animation
campFireView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"campFire01.gif"],
[UIImage imageNamed:#"campFire02.gif"],
[UIImage imageNamed:#"campFire03.gif"],
[UIImage imageNamed:#"campFire04.gif"],
[UIImage imageNamed:#"campFire05.gif"],
[UIImage imageNamed:#"campFire06.gif"],
[UIImage imageNamed:#"campFire07.gif"],
[UIImage imageNamed:#"campFire08.gif"],
[UIImage imageNamed:#"campFire09.gif"],
[UIImage imageNamed:#"campFire10.gif"],
[UIImage imageNamed:#"campFire11.gif"],
[UIImage imageNamed:#"campFire12.gif"],
[UIImage imageNamed:#"campFire13.gif"],
[UIImage imageNamed:#"campFire14.gif"],
[UIImage imageNamed:#"campFire15.gif"],
[UIImage imageNamed:#"campFire16.gif"],
[UIImage imageNamed:#"campFire17.gif"], nil];
// all frames will execute in 1.75 seconds
campFireView.animationDuration = 1.75;
// repeat the annimation forever
campFireView.animationRepeatCount = 0;
// start animating
[campFireView startAnimating];
// add the animation view to the main window
[self.view addSubview:campFireView];

Related

Imageview And/OR Sound having memory Issue (going in back ground)

My Application is in ARC.
In any view of my application there is one back ground image and on that image some animations are continuously repeating the images.
Problem is : My application is going in background after some time.
I seen and tried Lots of thing on stack, google and on apple doc but didn't got satisfactory solution.
I have Three Question.
(1) How to Stop this ?
(2) Is there any other method to release the memory manually in ARC.
(3) If i use [self.view removeFromSuperview]; for UIView then also some memory didn't released in ARC ?
MyCode for Animation is
imageview = [[UIImageView alloc] init];
imageview.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy08" ofType:#"png"]];
[self.view addSubview:imgBubbleAnimation];
imageview.animationImages = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy01" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy02" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy03" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy04" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy05" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy06" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bluecandy07" ofType:#"png"]],
nil];
[imageview setAnimationRepeatCount:0]; // 0 for continuous
imageview.animationDuration =3.0;
[imageview startAnimating];
And Sound files are (played when particular button clicked)
URL = [NSURL fileURLWithPath: [[NSBundle mainBundle]
pathForResource:#"ect1a" ofType:#"mp3"]];
audioSound = [[AVAudioPlayer alloc] initWithContentsOfURL:URL error:nil];
audioSound.delegate = self;
audioSound.volume = 5.0;
audioSound.numberOfLoops = 0;
[audioSound play];
I have 15 small sound files for one view and 15 animations for one view.
In every particular Animation there are min 15 images.
Any help, suggestion, link or tutorial are welcome.
Not using imageview.animationImages is the first thing you need to do to get your memory usage under control. That API should never be used as it lets you shoot yourself in the foot easily. See imageview-animation-getting-more-memory or unpredictable-crash-due-to-uiview-animations or iphone-app-memory-leak-with-uiimage-animation-problem-testing-on-device.

iOS UIImageView animationImages display remote pictures

I have three urls just like "http://xxx.x.xx.xx:8180/admin/upload/advert/advert_img.jpg",
they are pictures in the remote server.
And I have an UIImageView, I want to use:
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 504.0, 320.0, 40.0)];
NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:#"advert_img"],[UIImage imageNamed:#"img01"],[UIImage imageNamed:#"img02"], nil];
_imageView.animationImages = images;
_imageView.animationDuration = 15;
[_imageView startAnimating];
to display these pictures. How can I do this? Thank you very much.
Try this :
NSArray *frames = [NSArray arrayWithObjects:
[UIImage imageWithName:#"advert_img.png"],
[UIImage imageWithName:#"img01.png"],
[UIImage imageWithName:#"img02.png"],
nil];
UIImageView *animatedIMvw = [[UIImageView alloc] init];
animatedIMvw.animationImages = frames;
[animatedIMvw startAnimating];

Get UIImageView animation for at least one cycle before terminating [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm new to Objective C and X-code and I'm having trouble getting my UIImageView animation to stay on screen for at least one complete cycle. I am using it as a custom wait animation for when a button is tapped and a bunch of saving and processing stuff happens. I perform those actions in a separate thread using NSOpearationQueue. Here's the first part of my code:
My animation is defined in ViewDidLoad and it's called 'myAnimation'. it's a UIImageView animation made from an array of UIImages. I set it hidden immediately and show it when I need to.
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *myAnimationArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"arisprite_0000.png"],
[UIImage imageNamed:#"arisprite_0001.png"],
[UIImage imageNamed:#"arisprite_0002.png"],
[UIImage imageNamed:#"arisprite_0003.png"],
[UIImage imageNamed:#"arisprite_0004.png"],
[UIImage imageNamed:#"arisprite_0005.png"],
[UIImage imageNamed:#"arisprite_0006.png"],
[UIImage imageNamed:#"arisprite_0007.png"],
[UIImage imageNamed:#"arisprite_0008.png"],
[UIImage imageNamed:#"arisprite_0009.png"],
[UIImage imageNamed:#"arisprite_0010.png"],
[UIImage imageNamed:#"arisprite_0011.png"],
[UIImage imageNamed:#"arisprite_0012.png"],
[UIImage imageNamed:#"arisprite_0013.png"],
[UIImage imageNamed:#"arisprite_0014.png"],
[UIImage imageNamed:#"arisprite_0015.png"],
[UIImage imageNamed:#"arisprite_0016.png"],
[UIImage imageNamed:#"arisprite_0017.png"],
[UIImage imageNamed:#"arisprite_0018.png"],
[UIImage imageNamed:#"arisprite_0019.png"],
[UIImage imageNamed:#"arisprite_0020.png"],
[UIImage imageNamed:#"arisprite_0021.png"],
[UIImage imageNamed:#"arisprite_0022.png"],
[UIImage imageNamed:#"arisprite_0023.png"],
[UIImage imageNamed:#"arisprite_0024.png"],
[UIImage imageNamed:#"arisprite_0025.png"],
[UIImage imageNamed:#"arisprite_0026.png"],
[UIImage imageNamed:#"arisprite_0027.png"],
[UIImage imageNamed:#"arisprite_0028.png"],
[UIImage imageNamed:#"arisprite_0029.png"],
[UIImage imageNamed:#"arisprite_0030.png"],
[UIImage imageNamed:#"arisprite_0031.png"],
[UIImage imageNamed:#"arisprite_0032.png"],
[UIImage imageNamed:#"arisprite_0033.png"],
[UIImage imageNamed:#"arisprite_0034.png"],
[UIImage imageNamed:#"arisprite_0035.png"],
[UIImage imageNamed:#"arisprite_0036.png"],
[UIImage imageNamed:#"arisprite_0037.png"],
nil];
myAnimation.animationImages = myAnimationArray;
myAnimation.animationDuration = 2.5;
[myAnimation.layer setBorderColor:[[UIColor blackColor] CGColor]];
[myAnimation.layer setBorderWidth:2.0];
[self.view addSubview:myAnimation];
[myAnimation setHidden:YES];
...
...
...
}
-(IBAction)saveToYYY:(id)sender
{
// hide current view and show save screen
[self showAnimatedSaveScreen];
// show the saving screen
[self.view bringSubviewToFront:savingBackgroundView];
[self.view bringSubviewToFront:savingText];
[self.view bringSubviewToFront:myAnimation];
/* Operation Queue init */
NSOperationQueue *queue = [NSOperationQueue new];
/* Create our NSInvocationOperation to call save code, passing in nil */
NSInvocationOperation *saveToYYYOperation = [[NSInvocationOperation alloc]
initWithTarget:self
selector:#selector(saveToYYYWithOperation)
object:nil];
NSInvocationOperation *saveToXXXOperation = [[NSInvocationOperation alloc]
initWithTarget:self
selector:#selector(saveToXXXWithOperation)
object:nil];
/* Add the operation to the queue */
[saveToXXXOperation addDependency:saveToYYYOperation];
[queue addOperation:saveToYYYOperation];
[queue addOperation:saveToXXXOperation];
}
then, in my last queue operation i have
-(void)saveToXXXWithOperation
{
...
a bunch of processing
...
[self performSelectorOnMainThread:#selector(hideAnimatedSaveScreen) withObject:nil waitUntilDone:YES];
}
Everything seems to be working fine, except that when these save operations are too fast, especially on iPads, myAnimation doesn't get to finish it's duration of 2.5. I'd like to play at least once before I hide my animation. Is there a way to implement this?
If the question is "how can I wait 2.5 seconds after I finish saving", simply have your hideAnimatedSaveScreen implementation use delayed performance (performSelector:withObject:afterDelay:). You can use timestamps ([NSDate date]) to vary that amount, learning how much time has actually elapsed since the animation started and subtracting, as desired.
If the question is "how can I know when my animation is done", that's hard to say, since you don't actually show any animation in your code, so it's impossible to guess what you mean when you say you've got a UIImageView animation.

Xcode iPhone SDK - Show gif image on app Launch

I currently have a mp4 video converted from a gif playing at app launch, but using a video stops playing music and has problems with airplay devices connected.
Anyways what i really want is showing my gif on app launch. But i can't seem to get the gif to "play" it only shows one frame, how do i make it play the whole gif ?
My current code:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Show Animation;
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Launch" ofType:#"mp4"]];
LaunchPlayer = [[MPMoviePlayerViewController alloc]
initWithContentURL:url];
LaunchPlayer.view.frame = self.view.bounds;
LaunchPlayer.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
LaunchPlayer.moviePlayer.controlStyle = MPMovieControlStyleNone;
LaunchPlayer.view.tag = 1;
[self.view addSubview:LaunchPlayer.view];
[LaunchPlayer.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(LaunchFinish)
name:#"MPMoviePlayerPlaybackDidFinishNotification"
object:nil];
}
Although I don't see how the code you posted has anything to do with using a .gif, you its sticking to the first frame because iOS will not run an animated .gif. However, you can export each frame of your animation as a separate image and animate them like this.
-(void) viewDidLoad
{
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"myImageFrame1.gif"],
[UIImage imageNamed:#"myImageFrame2.gif"],
[UIImage imageNamed:#"myImageFrame3.gif"],
[UIImage imageNamed:#"myImageFrame4.gif"], nil];
imageView.animationDuration = 2.0;
imageView.animationRepeatCount = 0;
[imageView startAnimating];
}
In my little example here. i used a UIWEBVIEW called webView and had spinning-loader.gif added as a resource. The image can me put anywhere in the project as long as its added. This showed the gif as well as made it animate. Hope this helps.
NSString *pathImg = [[NSBundle mainBundle] pathForResource:#"spinning-loader" ofType:#"gif"];
NSString* webViewContent = [NSString stringWithFormat:
#"<html><body><img style='width:85;height:85;' src=\"file://%#\" /></body></html>", pathImg];
[webView loadHTMLString:webViewContent baseURL:nil];
webView.scrollView.bounces = NO;
webView.scrollView.scrollEnabled = NO;
webView.opaque=FALSE;
[webView setBackgroundColor:[UIColor clearColor]];

xcode: segmented control appears on sim, not on device

I have an iPad app with several segmented controls, including one view with two controls.
On that view, everything shows in the sim, both normal and retina. However, in the device, only one shows.
Below is the code that does not show on the device.. I have checked and all the images that make up the control are copied in the bundle resources. I've tried removing, cleaning, etc. No joy. I must be missing something (hopefully simple)..
UISegmentedControl *controls = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[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"],
nil]];
CGRect frame = CGRectMake(35, 70, 700, 35);
controls.frame = frame;
}
[controls addTarget:self action:#selector(drawSegmentAction:) forControlEvents:UIControlEventValueChanged];
controls.segmentedControlStyle = UISegmentedControlStyleBar;
controls.momentary = YES;
controls.tintColor = [UIColor grayColor];
[self.view addSubview:controls];
}
For reference, this code in the same view does work:
For reference, this control code does work:
-(void) buildColorBar {
//NSLog(#"%s", __FUNCTION__);
UISegmentedControl *colorControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects: [UIImage imageNamed:#"White.png"],
[UIImage imageNamed:#"Red.png"],
[UIImage imageNamed:#"Yellow.png"],
[UIImage imageNamed:#"Green.png"],
[UIImage imageNamed:#"Blue.png"],
[UIImage imageNamed:#"Purple.png"],
[UIImage imageNamed:#"Black.png"],
nil]];
NSLog(#"Portrait");
CGRect frame = CGRectMake(35, 950, 700, 35);
colorControl.frame = frame;
// When the user chooses a color, the method changeBrushColor: is called.
[colorControl addTarget:self action:#selector(changeBrushColor:) forControlEvents:UIControlEventValueChanged];
colorControl.segmentedControlStyle = UISegmentedControlStyleBar;
// Make sure the color of the color complements the black background
colorControl.tintColor = [UIColor grayColor];
// Add the control to the window
[self.view addSubview:colorControl];
}
Is there a rule against using two segmented controls in one view?
I found that one image in the segmented control - while it looked like it was in the bundle - it wasn't. PITA, but at least, it works..

Resources