Set activity indicator until image is loaded from server? - objective-c

I'm new to iOS development so please bear with me.
I'm making a photo-grid using a table view and scroll view.
My question is how could I load an activity indicator until an image downloads from a server and then display the image and remove the activity indicator?
I'm trying to stay away from third-party library's as I want to understand how it works.

Place an Activity Indicator (via Interface Builder or manually) on your view. Set the property to "hide when not animating".
When doing server call, call [activityIndicator startAnimating] (IBOutlet property).
When returning with actual image, call [activityIndicator stopAnimating]. When stopping, it wil automatically hide.
You can also use the activity indicator in the iPhon/Pad status bar. To do this, use [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
Set to NO for hiding it ... (obviously)
Take a look at dowloading an image async for a sample of dl'ing an image.
You would stop the activity indicator in the didReceiveData function.

https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCsQFjAA&url=https%3A%2F%2Fgithub.com%2Fjakemarsh%2FJMImageCache&ei=UEp7U_W2GoWB8gWShIGgBQ&usg=AFQjCNEc0a59K2wEOlZ2IbapWhVc87kHmg&bvm=bv.67229260,d.dGc
here you could find JMImage cache files that you could use for downloading images.
You have to change UIImageView+JMImageCache.m file
if(i) {
dispatch_async(dispatch_get_main_queue(), ^{
safeSelf.jm_imageURL = nil;
safeSelf.image = i;
[safeSelf setNeedsLayout];
[safeSelf setNeedsDisplay];
if (completionBlock) {
completionBlock(i).
}
});
then use method
enter code here
{
[yourImageView setImageWithURL:[NSURL URLWithString:urlString] placeholder:[UIImage imageNamed:#"placeholder"] completionBlock:^(UIImage *image)
{
// remove added activity indicator here
}failureBlock:^(NSURLRequest *req,NSURLResponse *resp,NSError *error)
{
// show error message
}];
}

Related

Progressview not showing

Strange things are happening here. Im working on a project which has a tableviewcontroller. The headersview are from a custom class which add a progressview and a button. When the button is clicked the progressview shows the download status en removes itself from the screen when done. Here is the problem.
The progressview isn't showing nor is it showing its progress (blue color). When I set the backgroundcolor to black I do see a black bar with no progress. Even when I set the progress to a static 0.5f.
Im also working with revealapp which shows the progressviews frame but not the trackingbar.
So the progressview is physically there but just not visible. Does anybody know why?
Structure:
Header.m
init:
[[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]
layoutSubviews:
CGRectMake(x, x, x, x);
Then:
- (void)synchMedia
{
// add progressview to view
[self performSelectorInBackground:#selector(synchMediaThread) withObject:nil];
}
- (void)synchMediaThread
{
// Do all the heavy lifting
// set progressview progress
// loop this:
[self performSelectorOnMainThread:#selector(updateProgressView:) withObject:progress waitUntilDone:YES];
// finally
[self performSelectorOnMainThread:#selector(synchDone:) withObject:#(numFailed) waitUntilDone:YES];
}
When done:
- (void)synchDone {
// remove progressview
[tableview reloadData];
}
Have you downloading any large data? If so then in that case updation on UI get blocked as main thread get busy.
To check this just add NSLog statement in a function which is calculating the progress of downloading. in this case it will log the progress on console but not on UI. If you get the correct progress on console then it is sure that UI updation is blocking when you start downloading.
Try to use some delay to update the UI or
dispatch_async(dispatch_get_main_queue(), ^{
//do you code for UI updation
});
The problem was with the way the UIView was used in the tableview delegate.

Check if NSAnimationContext runAnimationGroup cancelled or succeeded

I'm animating a view (by revealing it) after which I need to post a notification (once the animation completes). However the way the app's designed, there's another notification sent out when the view is hidden (via animation). So essentially I have a 'showView' and a 'hideView' method. Each do something like so:
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
[context setDuration: 0.25];
[[myView animator] setAlphaValue: 0];
} completionHandler:^{
// post notification now
}];
The problem is that in another thread in the background, I have a periodic timer which performs a 'sanity check' to see if the view needs to be hidden if it's not needed. It's an aggressive approach but necessary as many components of the app respond to different events and scenarios. Due to this I at times get a 'race condition' where the user has clicked to 'showView' but at the same time one of our threads feel the view should be immediately hidden.
When both post a notification on the main thread, the app hangs indefinitely in a SpinLock. I could completely avoid this situation if I was able to figure out if the animation block above was 'cancelled' (i.e. another animation block executed on the same view). In such situations I would not post the notification, which would then not force a check.
Long story short: I need to be able to check if the 'completionHandler' was called after animation successfully ended or if it was cancelled. I know in iOS this is possible but I can't find any way to do this in OS X. Please help.
I encountered a similar problem. I solved it by using a currentAnimationUUID instance variable.
NSUUID *animationUUID = [[NSUUID alloc] init];
self.currentAnimationUUID = animationUUID;
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
// set properties to animate
} completionHandler:^{
BOOL isCancelled = ![self.currentAnimationUUID isEqual:animationUUID];
if (isCancelled) {
return;
}
self.currentAnimationUUID = nil;
// do stuff
}];
Swift version:
let animationUUID = UUID()
currentAnimationUUID = animationUUID
NSAnimationContext.runAnimationGroup({ context in
// set properties to animate
}, completionHandler: {
let isCancelled = self.currentAnimationUUID != animationUUID
guard !isCancelled else {
return
}
self.currentAnimationUUID = nil
// do stuff
})

How to use MKMapView finished loading delegate, possible "finished displaying" delegate?

I'm attempting to save a thumbnail of a mapview when a user taps save when an annotation has been selected. The problem occurs when the user has not zoomed in on that annotation yet, so the close zoom level has not been loaded.
This is what I'm doing after the user taps save:
Set a bool "saving" to true
Center and zoom in on the annotation (no animation)
When the mapViewDidFinishLoadingMap delegate method gets called, and if saving is true:
Create an UIImage out of the view, and save it. Dismiss modal view.
However when the image is saved, and the view is dismissed the result image saved actually has not finished loading, as I still see an unloaded map with gridlines as shown below:
My question is, how can I ensure the map is finished loading AND finished displaying before I save this thumbnail?
Update: iOS7 has a new delegate which may have fixed this problem. I have not confirmed one way or the other yet.
- (void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered
Pre iOS6 support:
mapViewDidFinishLoadingMap: appears to be unreliable. I notice that it is sometimes not called at all, especially if the map tiles are already cached, and sometimes it is called multiple times.
I notice that when it is called multiple times the last call will render correctly. So I think you can get this to work if you set up a 2 second timer after the user taps save. Disable interactions so that nothing else can happen, and enable user interactions when the timer goes.
If mapViewDidFinishLoadingMap gets called reset the timer again for 2 seconds in the future. When the timer finally goes off, get the snapshot of the map and it should be correct.
You will also want to consider the other callbacks such as mapViewDidFailLoadingMap. Also test this on a noisy connection, since 2 seconds may not be long enough if it takes a long time to fetch the tiles.
- (void)restartTimer
{
[self.finishLoadingTimer invalidate];
self.finishLoadingTimer = [NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:#selector(mapLoadingIsFinished)
userInfo:nil
repeats:NO];
}
- (void)userClickedSave
{
assert(self.saving == NO);
if (self.saving == NO) {
self.saving = YES;
assert(self.finishLoadingTimer == nil);
self.view.userInteractionEnabled = NO;
[self restartTimer];
}
}
- (void)mapLoadingIsFinished
{
self.finishLoadingTimer = nil;
[self doSnapshotSequence];
self.saving = NO;
self.view.userInteractionEnabled = YES;
}
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
{
if (self.saving) {
[self restartTimer];
}
}
If developing for iOS7 the best delegate to use is: mapViewDidFinishRenderingMap:fullyRendered:
mapViewDidFinishRenderingMap:fullyRendered
Are you sure the area where you are taking the screenshot has the Zoom Level supported which you are applying. For example, in US zoom level support is higher, you can zoom in to the maximum detail, while in Asia may be a high zoom level might not be supported.

Save position in UIImagePickerController

I am developing an app which allows you to select photos and place them into different positions. The workflow is basically:
Tap an area of the screen
UIImagePickerController displays
Select a photo
Photo displays in the tapped area of the screen
I would like it so that if the user goes through this workflow for a second time, the UIImagePickerController when displayed will be showing the same album, and position within that album, that the user was last at.
I've tried saving a reference to the UIImagePickerController, as well as the UIPopoverController, so that they are created only once. However, every time I present the popover containing the UIImagePickerController, it is back at the main photos menu (eg. Camera Roll, Photo Library, My Photo Stream).
Any ideas for how to achieve what I'm after?
You can use ALAssetsLibrary . But this will cost you more effort. First time use – enumerateGroupsWithTypes:usingBlock:failureBlock: to list all album and remember user's choice. And at second time. Just use that album:ALAssetsGroup's – enumerateAssetsUsingBlock: to list all the images and videos. Apple has a few demo you can have a look PhotosByLocation MyImagePicker
keep a UIImagePickerController obj in .h class (for example imagePicker)
alloc the obj once (for example in viewDidLoad)
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self.view addSubview:imagePicker.view];
imagePicker.view.hidden = YES;
imagePicker.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
imagePicker.view.bounds = CGRectMake(0,20,self.view.frame.size.width, self.view.frame.size.height);
In didFinishPickingMediaWithInfo
if([[info valueForKey:UIImagePickerControllerMediaType] isEqualToString:#"public.image"]){
imagePicker.view.hidden = YES;
}
When you want to show the imagePickerView just do
imagePicker.view.hidden = NO;
Just to point you to right direction. You can use asset library to show the images as a picker. You can use the apple sample code MyImagePicker. The method [[assetsLibrary] enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) can be used for photo album. Using the asset library you can check which image was selected last and then use the method,
- (void)enumerateAssetsAtIndexes:(NSIndexSet *)indexSet options:(NSEnumerationOptions)options usingBlock:(ALAssetsGroupEnumerationResultsBlock)enumerationBlock;
You can use this method next time to enumerate which image onwards you want to enumerate. This method can accept an indexSet as [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(index, count)] which should help to indicate the last selected image.
To know more about how to use asset library check this.
It should be possible to reach into the resulting UITableView and then find its content offset. You can do this by searching the subviews of the UIImagePickerController's view property for a table view.
for (UIView *view in controller.view) {
if ([view isKindOfClass:[UITableView class]]) {
contentOffset = [(UITableView *)view contentOffset];
}
}
When you represent the view controller, you will want to restore the content offset in a similar fashion.
Note, I haven't actually tested to see the view hierarchy of the UIImagePickerController. Verify its structure by printing its subviews. There is also no guarantee that the structure will stay the same, since you are diving into the private implementation (though it's important to note you are not actually using any private APIs so this is okay).
Use AlAssetsLibrary. It control the image & video capture under the application. there a demo on apple.
https://developer.apple.com/library/ios/#documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html
look for this or
if you want to make a cutomize album for the image and video here a great example.
https://github.com/Kjuly/ALAssetsLibrary-CustomPhotoAlbum

MPMoviePlayerController adding UIButton to view that fades with controls

I am trying to add a UIButton to the view of a MPMoviePlayerController along with the standard controls. The button appears over the video and works as expected receiving touch events, but I would like to have it fade in and out with the standard controls in response to user touches.
I know I could accomplish this by rolling my own custom player controls, but it seems silly since I am just trying to add one button.
EDIT
If you recursively traverse the view hierarchy of the MPMoviePlayerController's view eventually you will come to a view class called MPInlineVideoOverlay. You can add any additional controls easily to this view to achieve the auto fade in/out behavior.
There are a few gotchas though, it can sometimes take awhile (up to a second in my experience) after you have created the MPMoviePlayerController and added it to a view before it has initialized fully and created it's MPInlineVideoOverlay layer. Because of this I had to create an instance variable called controlView in the code below because sometimes it doesn't exist when this code runs. This is why I have the last bit of code where the function calls itself again in 0.1 seconds if it isn't found. I couldn't notice any delay in the button appearing on my interface despite this delay.
-(void)setupAdditionalControls {
//Call after you have initialized your MPMoviePlayerController (probably viewDidLoad)
controlView = nil;
[self recursiveViewTraversal:movie.view counter:0];
//check to see if we found it, if we didn't we need to do it again in 0.1 seconds
if(controlView) {
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[controlView addSubview:backButton];
} else {
[self performSelector:#selector(setupAdditionalControls) withObject:nil afterDelay:0.1];
}
}
-(void)recursiveViewTraversal:(UIView*)view counter:(int)counter {
NSLog(#"Depth %d - %#", counter, view); //For debug
if([view isKindOfClass:NSClassFromString(#"MPInlineVideoOverlay")]) {
//Add any additional controls you want to have fade with the standard controls here
controlView = view;
} else {
for(UIView *child in [view subviews]) {
[self recursiveViewTraversal:child counter:counter+1];
}
}
}
It isn't the best solution, but I am posting it in case someone else is trying to do the same thing. If Apple was to change the view structure or class names internal to the control overlay it would break. I am also assuming you aren't playing the video full screen (although you can play it fullscreen with embeded controls). I also had to disable the fullscreen button using the technique described here because the MPInlineVideoOverlay view gets removed and released when it is pressed: iPad MPMoviePlayerController - Disable Fullscreen
Calling setupAdditionalControls when you receive the fullscreen notifications described above will re-add your additional controls to the UI.
Would love a more elegant solution if anyone can suggest something other than this hackery I have come up with.
My solution to the same problem was:
Add the button as a child of the MPMoviePlayerController's view;
fade the button in and out using animation of its alpha property, with the proper durations;
handle the player controller's touchesBegan, and use that to toggle the button's visibility (using its alpha);
use a timer to determine when to hide the button again.
By trial-and-error, I determined that the durations that matched the (current) iOS ones are:
fade in: 0.1s
fade out: 0.2s
duration on screen: 5.0s (extend that each time the view is touched)
Of course this is still fragile; if the built-in delays change, mine will look wrong, but the code will still run.