I want to implement startup loader in my app. It should be like this: after startup splash screen, user will watch simple animataion and in meanwhile app preload sound effects, background music, sprite images, spritesheets and so on. Current implementation:
- (id)init {
if((self = [super init])) {
// Some other setup ...
CGRect rect;
rect = waveSprite.textureRect;
waveInitialTexRectOrigin = rect.origin;
rect.size.width = 91;
waveSprite.textureRect = rect;
assetFilenames = [[NSArray alloc] initWithObjects:
// images
#"background.png",
// spritesheets
#"sprites.plist",
// fonts
#"main.png",
// sound effects
#"button.wav",
nil];
assetCounter = 0;
[self loadAsset];
}
return self;
}
- (void)update:(ccTime)dt {
CGRect rect;
rect = waveSprite.textureRect;
rect.origin.x += dt*kLoaderWaveSpeed;
while (rect.origin.x > waveInitialTexRectOrigin.x + kLoaderWavePeriod) {
rect.origin.x -= kLoaderWavePeriod;
}
waveSprite.textureRect = rect;
}
#pragma mark Private
- (void)loadAsset {
// CCLOG(#"loadAsset");
NSString *filename = [assetFilenames objectAtIndex:assetCounter];
CCLOG(#"loading %#", filename);
NSString *ext = [filename pathExtension];
if ([ext doesMatchRegStringExp:#"[png|jpg]"]) {
[[CCTextureCache sharedTextureCache] addImage:filename];
} else if ([ext isEqualToString:#"plist"]) {
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:filename];
} else if ([ext doesMatchRegStringExp:#"[caf|wav|mp3]"]) {
[[SimpleAudioEngine sharedEngine] preloadEffect:filename];
}
assetCounter++;
if (assetCounter < [assetFilenames count]) {
[self performSelector:#selector(loadAsset) withObject:self afterDelay:0.1f];
} else {
[self performSelector:#selector(loadingComplete) withObject:self afterDelay:0.2];
}
But the animation is SO abrupt.
UPD I've already tried
[self performSelectorInBackground: withObject:]
but it didn't seem to work (hung on loading first asset). Maybe I should try better in this direction.
UPD2 Smooth = not abrupt, without delays and flicker. fps doesn't matter, 20 fps quite OK
I'm not experienced with CC2D, but here's what I used in my ViewController to show a smooth animated loading indicator:
//Set up and run your loading scene/indicator here
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
//Load your scene
dispatch_async( dispatch_get_main_queue(), ^{
//This fires when the loading is complete
//Show menu and destroy the loader scene/indicator here
});
});
I guess it's worth a try.
You should not be using performSelectorInBackground anymore, it is not deprecated but it might be soon, with GCD now in the picture.
What I would try is run the animation in the main thread since it is the only one that can do UI updates. And spawn one or more threads using GCD to load all your assets.
I would use concurrent dispatch queues to load everything in parallel and simply dismiss the animation when this is done.
You can get complete information about concurrent dispatch queues here:
Concurrency Programming Guide
Hope that helps.
That is because you are doing everything on the same thread. Use a separate thread for the loader.
Hope this helps.
Cheers!
EDIT : Take a look at this.
Related
I've encountered a very interesting example, that I am so curios to reproduce.
There is a great app that I love a lot using:
https://itunes.apple.com/en/app/oldbooth/id298007500?mt=8
I an not advertising as I am not the author.
Now they have a interesting point, when from a table view, by selecting a thumb, a ViewController with a video session is loaded.
What I've noticed:
1) The loading of the video session screen is really fast, almost no waiting.
2) No UIImagePicker shutter animation present
3) The VC appearance animations looks like "pushVC", I mean it is horizontal
4)the view with preview is firstly white, then with an alpha animation it goes to 0 alpha.
I have some experience using AVFoundation video capturing, but it is not as quick! It is incredible for me, and I see that this level of speed is possible, ofcourse I want the same.. Please could you please advice how it is possible to achieve such level of performance?
I am trying the following:
In parent VC, in didLoad:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]];
self.photoVC = [storyboard instantiateViewControllerWithIdentifier:#"PhotoCreationVC"];
dispatch_async(queue, ^{
[self.photoVC configureFoundation];
});
The method does the following:
-(void) configureFoundation{
NSError * error;
_captureManager = [[AVCamDemoCaptureManager alloc] init];
if ([_captureManager setupSessionWithPreset:AVCaptureSessionPresetHigh error:&error]) {
[self setCaptureManager:_captureManager];
_sessionEstablished = YES;
} else {
[Utils showMessage:[error localizedDescription] withTitle:#"Input Device Init Failed"];
}
}
and when the VC runs didLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
if (_sessionEstablished) {
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:[_captureManager session]];
UIView *view = self.previewView;
CALayer *viewLayer = view.layer;
CGRect bounds = view.bounds;
[captureVideoPreviewLayer setFrame:bounds];
if ([captureVideoPreviewLayer isOrientationSupported]) {
[captureVideoPreviewLayer setOrientation:AVCaptureVideoOrientationPortrait];
}
[captureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
self.captureVideoPreviewLayer = captureVideoPreviewLayer;
if ([[_captureManager session] isRunning]) {
[_captureManager setDelegate:self];
NSUInteger cameraCount = [_captureManager cameraCount];
if (cameraCount < 1) {
} else if (cameraCount < 2) {
}
if (cameraCount < 1) {
}
[viewLayer insertSublayer:captureVideoPreviewLayer below:[[viewLayer sublayers] objectAtIndex:0]];
} else {
[Utils showMessage:#"Failed to start session." withTitle:#"Error"];
}
}
}
So what happens:
in the iTunes app it firstly loads the screen, then releases the alpha of the white view (i guess it is just cover view, which starts to lose alfa after a delay).
In my case, it still thinks a while before loading the VC. The time (1.5 seconds) is the same, but the waiting is not so friendly.
If i put all the loading code in didAppear, it loads immediately, but the loads, so the total time between tap and ready-video increases to 2.5 secs, which is not good.
If i put the loading code in willAppear, it works the same as in the first situation, when it first thinks, the loads (total 1.5 secs)...
PS I use AVCamDemoCaptureManager sample code from Apple, simplified, without any audio and movie output. just video input device and still image capturing.
Guys, please, can you give me some performance advice? how it is possible to achieve some high performance in loading a camera session?
Any help will be highly appreciated!
My application will query a database 50 times and will then react accordingly by adding the right UIImageView to another UIImageView, which I then want the application to display immediately at each loop.
Alas, after many nights I have failed to get it to work. The application will not immediately display the UIImageView. Having said that, when I zoom in or zoom out during mid loop the UIImageViews will appear! I must be missing something...
So far every code works except the last part [UIScrollView performSelector....
Please help and thank you in advance.
UIScrollView *firstView;
UIImageView *secondView;
UIImageView *thirdView;
NSOperationQueue *queue;
NSInvocationOperation *operation;
- (void)viewDidAppear:(BOOL)animated
{
queue = [NSOperationQueue new];
operation = [[NSInvocationOperation alloc] initWithTarget:self selector:#selector(getData) object:nil];
[queue addOperation:operation];
}
- (void) getData
{
for (i=0 ; i < 50 ; i++)
{
//All the codes to facilitate XML parsing here
[nsXMLParser setDelegate:parser];
[BOOL success = [nsXMLParser parse];
if (success)
{
if ([parser.currentValue isEqualToString:#"G"])
thirdView.image = greenTick.jpg;
[secondView addSubview:thirdView];
}
else
{
NSLog(#"Error parsing document!");
}
}
[thirdView performSelectorOnMainThread:#selector(setNeedsDisplay) withObject:nil waitUntilDone: YES];
}
I discovered the solution and truly hope this will help someone...
if (success)
{
if ([parser.currentValue isEqualToString:#"G"])
// Make the changes here - start
dispatch_queue_t main_queue = dispatch_get_main_queue();
dispatch_async(main_queue, ^{
thirdView.image = greenTick.jpg;
[secondView addSubview:thirdView];
});
// Make the changes here - end
}
else
{
NSLog(#"Error parsing document!");
}
}
// Remove performSelectorOnMainThread
I have started playing with UIProgressView in iOS5, but havent really had luck with it. I am having trouble updating view. I have set of sequential actions, after each i update progress. Problem is, progress view is not updated little by little but only after all have finished. It goes something like this:
float cnt = 0.2;
for (Photo *photo in [Photo photos]) {
[photos addObject:[photo createJSON]];
[progressBar setProgress:cnt animated:YES];
cnt += 0.2;
}
Browsing stack overflow, i have found posts like these - setProgress is no longer updating UIProgressView since iOS 5, implying in order for this to work, i need to run a separate thread.
I would like to clarify this, do i really need separate thread for UIProgressView to work properly?
Yes the entire purpose of progress view is for threading.
If you're running that loop on the main thread you're blocking the UI. If you block the UI then users can interact and the UI can't update. YOu should do all heavy lifting on the background thread and update the UI on the main Thread.
Heres a little sample
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelectorInBackground:#selector(backgroundProcess) withObject:nil];
}
- (void)backgroundProcess
{
for (int i = 0; i < 500; i++) {
// Do Work...
// Update UI
[self performSelectorOnMainThread:#selector(setLoaderProgress:) withObject:[NSNumber numberWithFloat:i/500.0] waitUntilDone:NO];
}
}
- (void)setLoaderProgress:(NSNumber *)number
{
[progressView setProgress:number.floatValue animated:YES];
}
Define UIProgressView in .h file:
IBOutlet UIProgressView *progress1;
In .m file:
test=1.0;
progress1.progress = 0.0;
[self performSelectorOnMainThread:#selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];
- (void)makeMyProgressBarMoving {
NSLog(#"test %f",test);
float actual = [progress1 progress];
NSLog(#"actual %f",actual);
if (progress1.progress >1.0){
progress1.progress = 0.0;
test=0.0;
}
NSLog(#"progress1.progress %f",progress1.progress);
lbl4.text=[NSString stringWithFormat:#" %i %%",(int)((progress1.progress) * 100) ] ;
progress1.progress = test/100.00;//actual + ((float)recievedData/(float)xpectedTotalSize);
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(makeMyProgressBarMoving) userInfo:nil repeats:NO];
test++;
}
I had similar problem. UIProgressView was not updating although I did setProgress and even tried setNeedsDisplay, etc.
float progress = (float)currentPlaybackTime / (float)totalPlaybackTime;
[self.progressView setProgress:progress animated:YES];
I had (int) before progress in setProgress part. If you call setProgress with int, it will not update like UISlider. One should call it with float value only from 0 to 1.
All,
I am attempting to load a set of sounds asynchronously when I load a UIViewController. At about the same time, I am (occasionally) also placing a UIView on the top of my ViewController's hierarchy to present a help overlay. When I do this, the app crashes with a bad exec. If the view is not added, the app does not crash. My ViewController looks something like this:
- (void)viewDidLoad
{
[super viewDidLoad];
__soundHelper = [[SoundHelper alloc] initWithSounds];
// Other stuff
}
- (void)viewDidAppear:(BOOL)animated
{
// ****** Set up the Help Screen
self.coachMarkView = [[FHSCoachMarkView alloc] initWithImageName:#"help_GradingVC"
coveringView:self.view
withOpacity:0.9
dismissOnTap:YES
withDelegate:self];
[self.coachMarkView showCoachMarkView];
[super viewDidAppear:animated];
}
The main asynchronous loading method of SoundHelper (called from 'initWithSounds') looks like this:
// Helper method that loads sounds as needed
- (void)loadSounds {
// Run this loading code in a separate thread
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
NSBlockOperation *loadSoundsOp = [NSBlockOperation blockOperationWithBlock:^{
// Find all sound files (*.caf) in resource bundles
__soundCache = [[NSMutableDictionary alloc]initWithCapacity:0];
NSString * sndFileName;
NSArray *soundFiles = [[NSBundle mainBundle] pathsForResourcesOfType:STR_SOUND_EXT inDirectory:nil];
// Loop through all of the sounds found
for (NSString * soundFileNamePath in soundFiles) {
// Add the sound file to the dictionary
sndFileName = [[soundFileNamePath lastPathComponent] lowercaseString];
[__soundCache setObject:[self soundPath:soundFileNamePath] forKey:sndFileName];
}
// From: https://stackoverflow.com/questions/7334647/nsoperationqueue-and-uitableview-release-is-crashing-my-app
[self performSelectorOnMainThread:#selector(description) withObject:nil waitUntilDone:NO];
}];
[operationQueue addOperation:loadSoundsOp];
}
The crash seems to occur when the block exits. The init of FHSCoachMarkView looks like this:
- (FHSCoachMarkView *)initWithImageName:(NSString *) imageName
coveringView:(UIView *) view
withOpacity:(CGFloat) opacity
dismissOnTap:(BOOL) dismissOnTap
withDelegate:(id<FHSCoachMarkViewDelegate>) delegateID
{
// Reset Viewed Coach Marks if User Setting is set to show them
[self resetSettings];
__coveringView = view;
self = [super initWithFrame:__coveringView.frame];
if (self) {
// Record the string for later reference
__coachMarkName = [NSString stringWithString:imageName];
self.delegate = delegateID;
UIImage * image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:#"png"]];
// ****** Configure the View Hierarchy
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
[self addSubview:imgView];
[__coveringView.superview insertSubview:self aboveSubview:__coveringView];
// ****** Configure the View Hierarchy with the proper opacity
__coachMarkViewOpacity = opacity;
self.hidden = YES;
self.opaque = NO;
self.alpha = __coachMarkViewOpacity;
imgView.hidden = NO;
imgView.opaque = NO;
imgView.alpha = __coachMarkViewOpacity;
// ****** Configure whether the coachMark can be dismissed when it's body is tapped
__dismissOnTap = dismissOnTap;
// If it is dismissable, set up a gesture recognizer
if (__dismissOnTap) {
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(coachMarkWasTapped:)];
[self addGestureRecognizer:tapGesture];
}
}
return self;
}
I have tried invoking the asynchronous block using both NSBlockOperation and dispatch_async and both have had the same results. Additionally, I've removed the aysnch call altogether and loaded the sounds on the main thread. That works fine. I also tried the solution suggested by #Jason in: NSOperationQueue and UITableView release is crashing my app but the same thing happened there too.
Is this actually an issue with the view being added in FHSCoachMarkView, or is it possibly related to the fact that both access mainBundle? I'm a bit new to asynch coding in iOS, so I'm at a bit of a loss. Any help would be appreciated!
Thanks,
Scott
I figured this out: I had set up a listener on the SoundHelper object (NSUserDefaultsDidChangeNotification) that listened for when NSUserDefaults were changed, and loaded the sounds if the user defaults indicated so. The FHSCoachMarkView was also making changes to NSUserDefaults. In the SoundHelper, I was not properly checking which defaults were being changed, so the asynch sound loading method was being called each time a change was made. So multiple threads were attempting to modify the __soundCache instance variable. it didn't seem to like that.
Question: Is this the correct way to answer your own question? Or should I have just added a comment to the question it self?
Thanks.
I'm having a hard time removing my animation from memory. What is the best way to do this?
This is my animation code:
-(IBAction)animationOneStart {
NSMutableArray *images = [[NSMutableArray alloc] initWithCapacity:88];
for(int count = 1; count <= 88; count++)
{
NSString *fileName = [NSString stringWithFormat:#"testPic1_%03d.jpg", count];
UIImage *frame = [UIImage imageNamed:fileName];
[images addObject:frame];
}
loadingImageView.animationImages = images;
loadingImageView.animationDuration = 5;
loadingImageView.animationRepeatCount = 1; //Repeats indefinitely
[loadingImageView startAnimating];
[images release];
}
Thanks!
[images removeAllObjects];
[images release];
Don't need to release it anymore with ARC under Xcode 4.2! Update today :)
As far as I can tell, there is not a callback to know when the imageView's animation is finished. This is unfortunate, because it means that to remove the animation when the imageView is finished animating, you will need repeatedly check the imageView's isAnimating method until it returns false. The following is an example of what I mean:
You might be able to do something like this—in your original code, add the following line
[loadingImageView startAnimating];
[images release];
[self performSelector:#selector(checkIfAnimationIsFinished) withObject:nil afterDelay:4.5f];
Then create a method
-(void)checkIfAnimationIsFinished {
if (![loadingImageView isAnimating]) {
[loadingImageView release];
} else {
[self performSelector:#selector(checkIfAnimationIsFinished) withObject:nil afterDelay:0.5f];
}
}
This was just an example, I used the values 4.5f and 0.5f because, unfortunately, the timing is not precise, it is more of a ballpark figure. 4.9f and 0.1f might work better. Regardless, the problem is that if you set the method to fire 5.0f seconds after you start the animation, it is not 100% certain that the animation will be be finished or not. So you will need to check repeatedly if it is not finished.
Anyway, this is not the cleanest solution, but unless someone else knows how to get a callback when an imageView is finished animating, I don't know of a better solution.