I have created a simple UIActivityIndicatorView that takes care of informing the user about the end of the execution for a specific task. My implementation is the following:
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
spinner.center = self.imageView.center;
[self.imageView addSubview:spinner];
[spinner startAnimating];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
UIImage *filteredImage = ...some filtering...;
self.imageView.image = filteredImage;
[self.imageView setNeedsDisplay];
dispatch_async(dispatch_get_main_queue(), ^{
[spinner stopAnimating];
});
});
However, when I run the application, the spinning wheel disappears and after a while the image is updated. Do you have any hint about how there is this delay?
EDIT: the setNeedsDisplay and the stopAnimating instructions are called in the right order. However, the UIImageView takes a while in order to update its content.
Thank you in advance.
The problem with your code is-
You are trying to assigning image on imageview on other thread instead of main thread (GUI thread) so it takes time.
To resolve this I have made few changes in your code-
Instead of using this-
UIImage *filteredImage = ...some filtering...;
self.imageView.image = filteredImage;
[self.imageView setNeedsDisplay];
Try this-
self.imageView.image = filteredImage;
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = filteredImage;
[self.imageView setNeedsDisplay];
});
I have also faced this, hope it helps you.
Related
I am using AsyncImageView classes to apply lazy loading on UITableView. And want to apply activity indicator on image view until the image is loaded on cell. Below is my code i am trying.
// AsyncIamgeView.m
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
//[connection release];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
indicator.center = CGPointMake(15, 15);
connection=nil;
if ([[self subviews] count]>0) {
[[[self subviews] objectAtIndex:0] removeFromSuperview];
}
UIImage *imgData = [UIImage imageWithData:data];
UIImageView* imageView = [[UIImageView alloc]init];
[imageView addSubview:indicator];
[indicator startAnimating];
if(imgData == nil)
{
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"NoImagenew.png"]];
//[indicator stopAnimating];
}
else{
imageView = [[UIImageView alloc] initWithImage:imgData];
// [indicator stopAnimating];
}
//imageView.contentMode = UIViewContentModeScaleAspectFit;
//imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth || UIViewAutoresizingFlexibleHeight );
//[imageView sizeToFit];
[self addSubview:imageView];
imageView.frame = self.bounds;
//imageView.frame = CGRectMake(0, 0, 85, 94);
[imageView setNeedsLayout];
[self setNeedsLayout];
//[data release];
data=nil;
}
// cellForRowAtIndexPath method.
asyncImageView = [[AsyncImageView alloc]initWithFrame:CGRectMake(1, 3, 85, 54)];
[asyncImageView loadImageFromURL:[NSURL URLWithString:imageUrlString]];
[cell.contentView addSubview:asyncImageView];
This code shows activity indicator but when image is loaded after that not before loading images. Please guide for above.
You have to create AsyncImageView object instead of UIImageView, then it will automatically add indicator to your view
AsyncImageView *imageView = [[AsyncImageView alloc] initWithFrame:CGRectMake(1, 3, 85, 54)];
[cell addSubview:imageView];
//cancel loading previous image for cell
[[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:imageView];
//load the image
imageView.imageURL = [imageURLs objectAtIndex:indexPath.row];
Here is an example of this
Currently you are adding activity indicator when your image is downloaded.
Here is the simple idea, hope you can implement this in your code
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// This method is called when your connection receives a response
// add your activity indication here and start animating
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// This method is called when your image is downloaded.
// remove your activity indicator or stop animating here
}
Hope u downloaded AsyncImageView from https://github.com/nicklockwood/AsyncImageView. By default, it has the feature to show activity indicator. You just dont need to add or remove any code by yourself to get this feature works. Just call loadImageWithURL.
I think u are using asynimageview classs, In that by default you will get the loader in the cell itself .
AysncImageView already has a loader.
You won't see it if your background is black since it loads the default activity indicator.
So, just set the activityIndicatorStyle property for your AsyncImageView object based on your background.
In my case, my background was black, so I used the following code :
asyncImgView.activityIndicatorStyle = UIActivityIndicatorViewStyleWhite;
Dear stackoverflowers,
I have gone astray.
In my app I load 2 images from the web, like this:
-(void)loadImages
{
...
image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl1]];
image2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl2]];
}
In order to not blocking the main thread, I use GCD:
dispatch_async( dispatch_get_global_queue(0,0), ^{
[self loadImages];
After that, I use these images in my UITableView:
if (indexPath.row == 0)
{
cell.imageView.image = image1;
}
else
{
cell.imageView.image = image2;
}
Then I decided to add UIActivityIndicator, but faced some problems. I understand that my code is not correct. I saw that people using NSURLRequest and NSURLConnection to load images and add UIActivityIndicator.
Could you tell me please, what is the most standart way to load images like that ? What should I rewrite ?
I would suggest you to use AsyncImageView a beautiful implementation by Nicklockwood -father of iCarousel.
https://github.com/nicklockwood/AsyncImageView
it is very simple to use.
#import "AsyncImageView.h"
and in all imageViews do this.
[imageView setImage:#"default.png"];
[imageView setImageURL:#"whateverurl/whateverimage.png"];
In your case it would be:
[cell.imageView setImageURL:#"yourURL"];
It works like a charm, and my code is in production. But if you still want your code to work try this:
UIActivityIndicator *activity =[[UIActivityIndicator alloc] initWithStyle:UIActivityIndicatorWhite];
[activity setFrame:CGRectMake(100,100,30,30)];
[self.view addSubview:activity];
dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(dispatchQueue, ^(void)
{
[activity startAnimating];
[self loadImages];
dispatch_sync(dispatch_get_main_queue(), ^{
[yourTableView reloadData];
[activity stopAnimating];
[activty setHidden:YES];
});
});
I can tell you what I usually do if I want to use my own custom code
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSData * imageData = [NSData dataWithContentsOfURL:_pictureURL];
dispatch_async(dispatch_get_main_queue(), ^{
imageView.image = [UIImage imageWithData:imageData];
});
});
Otherwise you can consider the UIImageView+AFNetworking category that adds the method
setImageWithURLRequest:placeholderImage:success:failure:
to UIImageView.
There is nothing wrong with your code (assuming that you've confirmed that it works). Just because others use NSURLConnection and related doesn't make yours wrong.
To use the activity indicator do this:
-(void)loadImages
{
UIActivityIndicator *indicator = ...
...
[indicator startAnimating];
image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl1]];
image2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl2]];
[indicator stopAnimating];
}
Small nit - personally I'd use:
cell.imageView.image = (0 == indexPath.row ? image1 : image2)
as nobody seems to appreciate the C 'conditional expressoin'. End small nit.
I have an image picker to pick multiple images from the photo library. The picked photos are resized and saved to file in imagePickerController:didFinishPickingMediaWithInfo:. It takes a few seconds.
Could I put an indicator on the view?
I have tried to use addSubview:indicatorView in imagePickerController:didFinishPickingMediaWithInfo: method. But indicatorView doesn't appear. It gets dismissed with the imagePickerView.
You should add the UIActivityIndicatorView at the top of the view hierarchy or it will get dismissed with the UIPickerViewController, unless you use a callback after the resize operation and in the callback you dismiss the UIImagePickerController.
Or you could use a progress HUD like SVProgressHUD.
Hope this helps =)
We did a little chat session and solved this way:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
UIView *primaryImage = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,460)];
primaryImage.backgroundColor = [UIColor redColor];
primaryImage.alpha =0.9;
UIView *secondaryImage = [[UIView alloc] initWithFrame:CGRectMake(0,0,70,70)];
secondaryImage.center = CGPointMake(160, 240);
secondaryImage.backgroundColor = [UIColor blackColor];
secondaryImage.alpha = 0.9;
secondaryImage.layer.cornerRadius = 12;
[primaryImage addSubview:secondaryImage];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
indicator.center = CGPointMake(35, 35);
[indicator setHidesWhenStopped:NO];
[secondaryImage addSubview:indicator];
[indicator startAnimating];
[indicator release];
[secondaryImage release];
[picker.view addSubview:primaryImage];
[primaryImage release];
});
// Put here the code to resize the image
dispatch_async(dispatch_get_main_queue(), ^{
// Dismiss the picker controller
});
});
-(IBAction)actionPrevious:(id)sender{
[self startact];
pageNumber = pageNumber - 1;
if (pageNumber>0) {
NSString *str_Img =[array_Image objectAtIndex:pageNumber];
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:str_Img]];
UIImage *myimage = [[UIImage alloc] initWithData:mydata];
[imageView1 setImage:myimage];
[self.view addSubview:imageView1];
lbl_PhotoName.text = [array_Name objectAtIndex:pageNumber];
lbl_PhotoDate.text = [array_Date objectAtIndex:pageNumber];
lbl_PhotoDesc.text = [array_Desc objectAtIndex:pageNumber];
[mydata release];
[myimage release];
}
[self endact];
}
-(void)startact{
[act setHidden:NO];
[act startAnimating];
}
-(void)endact{
[act stopAnimating];
[act setHidden:YES];
}
In above code activity activity indicator is not display. Photo are display using the web service. please Help!
Thank You
You need to work on the same thread and need to call by this way
[self performSelector:#selector(startact) withObject:nil afterDelay:1];
You need to use threading in these kinds of scenarios.
Because activity indicator is on same thread as of the images work; thats why it is creating problem.
This is a silly mistake that I always seem to make: If you added the activity indicator programmatically did you make sure to addSubview: ? Or maybe it's hidden by something? Everything else looks fine, and you definitely don't need to startAnimating in a separate thread.
I have implemented a UIActivityIndicator that shows up in one part of my program but not another. I have the activity indicator come up while i am loading a table, however, i am trying to get it to start animating again after the user has clicked a button and is waiting for the table to reload. The table reloads, but no indicator. Here is the code.
- (void)viewWillAppear:(BOOL)animated {
CGRect frame = CGRectMake (120.0, 185.0, 80, 80);
activity = [[UIActivityIndicatorView alloc] initWithFrame: frame];
activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[activity startAnimating];
[navigationUpdateFromDetail.window addSubview: activity];
[super viewWillAppear:animated];
}
It comes up for that part. However, for the next part it does not want to seem to come up.
- (IBAction) btnGreaterTen_clicked :(id)sender {
self.searchDistance = [NSNumber numberWithDouble : 10];
CGRect frame = CGRectMake (120.0, 185.0, 80, 80);
activity = [[UIActivityIndicatorView alloc] initWithFrame: frame];
activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[navigationUpdateFromDetail.window addSubview: activity];
[activity startAnimating];
NSLog(#" search value after change %#", [searchDistance description]);
[self getSetDisplay];
[activity stopAnimating];
}
That button changes a variable and is suppose to start the animation, but it does not. I have changed the color to make sure it was not just blending in, so that is not the solution. I tried to recreate the same object, but still no luck.
Thank you in advance.
That won't work, because the animation will only show when the main application loop is "running". In your code, you're blocking the main thread by calling [self getSetDisplay].
You should load your data asynchronously to make this work (in a background thread). Then you can call startAnimating, start your thread, and when the thread finishes, stop the animation.
This has been bugging me for ages, and I just found that when using a search display controller I had to add the activity indicator as a subview of the search results to get it to show. In my viewDidLoad, I put...
// create activity indicator
activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
[self.searchDisplayController.searchResultsTableView addSubview:activityIndicator];
When I want to start animating, i put...
[activityIndicator setCenter:CGPointMake(self.searchDisplayController.searchResultsTableView.frame.size.width/2.0f, self.searchDisplayController.searchResultsTableView.frame.size.height/2.0f)];
[activityIndicator startAnimating];
Hope this helps someone.
try adding
activity.hidden = NO;
just before
[activity startAnimating];