I am trying to add an activity indicator into an imageView as follows when I am downloading the contents from url but the problem is that the activity indicator is not visible.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = nil;
if ([imageArray count] >0){
if(cachedImage[[imageArray objectAtIndex:indexPath.row]] != nil){
recipeImageView.image = cachedImage[[imageArray objectAtIndex:indexPath.row]];
}
else{
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(140, 236, 37, 37);
[activityIndicator startAnimating];
[recipeImageView addSubview:activityIndicator];
self.view.userInteractionEnabled = NO;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
NSData *data0 = [NSData dataWithContentsOfURL: [NSURL URLWithString:[imageArray objectAtIndex:indexPath.row]]];
UIImage *image = [UIImage imageWithData: data0];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
recipeImageView.image = image;
[activityIndicator stopAnimating];
cachedImage[[imageArray objectAtIndex:indexPath.row]] = image;
});
});
}
}
How can i be able to show the activity indicator within imageview with progress percent if possible?
Just update your code and try this
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = nil;
if ([imageArray count] >0){
if(cachedImage[[imageArray objectAtIndex:indexPath.row]] != nil){
recipeImageView.image = cachedImage[[imageArray objectAtIndex:indexPath.row]];
}
else{
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[activityIndicator setCenter: recipeImageView.center];
[activityIndicator startAnimating];
[cell.contentView addSubview:activityIndicator];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
NSData *data0 = [NSData dataWithContentsOfURL: [NSURL URLWithString:[imageArray objectAtIndex:indexPath.row]]];
UIImage *image = [UIImage imageWithData: data0];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
recipeImageView.image = image;
[activityIndicator removeFromSuperview];
cachedImage[[imageArray objectAtIndex:indexPath.row]] = image;
});
});
}
}
Try this:
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.center = recipeImageView.center;
[activityIndicator startAnimating];
[recipeImageView addSubview:activityIndicator];
self.view.userInteractionEnabled = NO;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
NSData *data0 = [NSData dataWithContentsOfURL: [NSURL URLWithString:[imageArray objectAtIndex:indexPath.row]]];
UIImage *image = [UIImage imageWithData: data0];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
recipeImageView.image = image;
[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];
cachedImage[[imageArray objectAtIndex:indexPath.row]] = image;
});
});
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[indicator startAnimating];
[indicator setCenter:self.recipeImageView.center];
[self.contentView addSubview:indicator];
Related
NSString *url = [NSString stringWithFormat:#"%#",[enclosureUrlArray objectAtIndex:indexPath.row]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
([self.tableView cellForRowAtIndexPath:indexPath]);
dispatch_async(dispatch_get_main_queue(), ^{
cell.tag = indexPath.row;
UIImage *image = [UIImage imageWithData:imageData];
cell.IMGLabel.image = image;
[cell.IMGLabel setImage:image];
[cell.IMGLabel.layer setMasksToBounds:YES];
[cell.IMGLabel.layer setCornerRadius:2.5f];
[cell setNeedsLayout];
});
});
I am using array of url's for images to load in collection view. Images are changing while scrolling the collection view. How to fix this issue?
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"RecentProductCell";
RecentProductCell *recentCell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
recentCell.recentProductImg.image = nil;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSString *imageurl = [[latestProducts valueForKey:#"image"]objectAtIndex:indexPath.item];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageurl]];
dispatch_sync(dispatch_get_main_queue(), ^{
recentCell.recentProductImg.image = [UIImage imageWithData:imageData];
});
});
}
Replace dispatch_sync(dispatch_get_main_queue(), ^{
recentCell.recentProductImg.image = [UIImage imageWithData:imageData];
});
with
dispatch_sync(dispatch_get_main_queue(), ^{
if ( imageData )
{
UIImage *urlImage = [[UIImage alloc] initWithData:imageData];
RecentProductCell *recentCell = (id)[collectionView cellForItemAtIndexPath:indexPath];
if (recentCell)
[recentCell.recentProductImg setImage:urlImage];
}
});
Let me know if this works for you.
cell.imageView.image = nil;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void) {
NSString *imageurl = [[latestProducts valueForKey:#"image"]objectAtIndex:indexPath.item];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageurl]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
if (cell.tag == indexPath.row) {
cell.imageView.image = image;
[cell setNeedsLayout];
}
});
}
});
Write this code in cellForRowAtIndexPath method of collectionView
/Problem is that, I created my own contentviews in UITableview and showing. While scrolling the table view is giving Jerking. How to resolve this issue./
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
/ImageDetails, I am receiving from server and loading using lazy loading./
UIImageView *lblImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 60, 60)];
lblImage.contentMode = UIViewContentModeScaleAspectFill;
[cell.contentView addSubview:lblImage];
if([[[lArray objectAtIndex:indexPath.row] objectForKey:#"Foto"] length] > 0){
NSURL *imageURL = [NSURL URLWithString:[[lMovikidListArray objectAtIndex:indexPath.row] objectForKey:#"MovikitFoto"]];
NSString *key = [[[lArray objectAtIndex:indexPath.row] objectForKey:#"Foto"] MD5Hash];
NSData *data = [FTWCache objectForKey:key];
if (data) {
UIImage *image = [UIImage imageWithData:data];
lblImage.image = image;
} else {
lblImage.image = [UIImage imageNamed:#"defaultprofile.png"];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSData *data = [NSData dataWithContentsOfURL:imageURL];
[FTWCache setObject:data forKey:key];
UIImage *image = [UIImage imageWithData:data];
dispatch_sync(dispatch_get_main_queue(), ^{
lblImage.image = image;
});
});
}
CALayer *imageLayer = lblImage.layer;
[imageLayer setCornerRadius:lblImage.frame.size.width/2];
[imageLayer setMasksToBounds:YES];
}
else{
lblImage.image = [UIImage imageNamed:#"defaultprofile.png"];
}
UILabel *lblTitle = [[UILabel alloc]initWithFrame:CGRectMake(75, 10, 220, 25)];
lblTitle.text = [NSString stringWithFormat:#"%# - %#", [[lArray objectAtIndex:indexPath.row] objectForKey:#"Name"], [[lArray objectAtIndex:indexPath.row] objectForKey:#"SimNo"]];
lblTitle.font = [UIFont boldSystemFontOfSize:14.0f];
[cell.contentView addSubview:lblTitle];
UILabel *lblSubTitle = [[UILabel alloc]initWithFrame:CGRectMake(100, 40, 195, 35)];
if([[[lArray objectAtIndex:indexPath.row] objectForKey:#"Location"] length] <= 1){
lblSubTitle.text = LOCATION_NOT_AVAILABLE;
lImageName = #"caution_icon.png";
}
else{
lblSubTitle.text = [self calculateDistance:<some data>];
}
lblSubTitle.textColor = [UIColor darkGrayColor];
lblSubTitle.numberOfLines = 2;
lblSubTitle.font = [UIFont italicSystemFontOfSize:12.0f];
[cell.contentView addSubview:lblSubTitle];
/This image is a small image to show some indicators./
UIImageView *lblIndicatorImage = [[UIImageView alloc]initWithFrame:CGRectMake(75, 45, 20, 20)];
lblIndicatorImage.image = [UIImage imageNamed:lImageName];
[cell.contentView addSubview:lblIndicatorImage];
[cell setNeedsDisplay];
You cant do any UIKit operation on background thread. So just remove your graphical operation from background thread and it will be fine.
else {
lblImage.image = [UIImage imageNamed:#"defaultprofile.png"];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSData *data = [NSData dataWithContentsOfURL:imageURL];
[FTWCache setObject:data forKey:key];
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithData:data];
lblImage.image = image;
});
});
(void)bilgileriYukle
{
NSMutableArray *yemekIsimleri = [[NSMutableArray alloc] init];
NSMutableArray *resimIsimleri = [[NSMutableArray alloc] init];
NSURL *myURL = [[NSURL alloc]initWithString:#"http://www.sevgilezzeti.com/WebServices/tarifler.php"];
NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL];
if (myData == nil)
{
NSLog(#"INTERNET YOK YADA VERI CEKEMEDI");
}
else
{
NSLog(#"INTERNET VAR VERI CEKILDI");
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:myData options:kNilOptions error:nil];
for (id element in jsonArray)
{
NSString *durum = [element objectForKey:#"durum"];
if([durum isEqualToString:#"1"])
{
[yemekIsimleri addObject:[element objectForKey:#"yemekadi"]];
NSString *gelenYemekAdi =[element objectForKey:#"kapakresmi"];
NSString *imagePathKapak = #"http://sevgilezzeti.com/Admin/yemekresimkapak/";
combined = [NSString stringWithFormat:#"%#%#", imagePathKapak, gelenYemekAdi];
[resimIsimleri addObject:combined];
}
}
}
_TarifAdi=yemekIsimleri;
_ResimAdi=resimIsimleri;
}
///////////////////////////////////////////////////////////////////////////////////////
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"TableViewCell" forIndexPath:indexPath];
NSUInteger row = [indexPath row];
cell.TitleLabel.text = _TarifAdi[row];
NSCache *_imageCache;
UIImage *image = [_imageCache objectForKey:#"DenemeRes"];
if (image == nil) {
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:_ResimAdi[row]]];
image = [UIImage imageWithData:data];
[_imageCache setObject:image forKey:#"DenemeRes"];
}
cell.ThumbImage.image = image;
return cell;
}
I can get images from URL and can see images but when I scroll the table view, it's very slow and lagging. How can I fix this problem ?
It's pretty easy and straightforward to load images synchronously in table view cells, however it's evil, because it would cause the lag when you scroll it, because it's actually trying to load and display the images as you're scrolling.
Therefore, you must avoid loading images synchronously, but asynchronously instead, using a different thread other than the main thread, so that the scrolling and loading + displaying images can be done dependently, avoiding any kind of lag.
This is possibly a duplicate question, since it's already been asked and there are tutorials out there, but since I myself always had a problem with this on my first iOS app and found it very confusing, I'm posting the answer here, hoping it's helpful.
Try adding something like this to your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath:
// Get the filename to load.
NSString *imageFilename = [imageArray objectAtIndex:[indexPath row]];
NSString *imagePath = [imageFolder stringByAppendingPathComponent:imageFilename];
[[cell textLabel] setText:imageFilename];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
dispatch_sync(dispatch_get_main_queue(), ^{
[[cell imageView] setImage:image];
[cell setNeedsLayout];
});
});
dataWithContentsOfURL is blocking your main thread, that'w why the table is acting like it is. You should load images in back thread so that the main thread can handle UI without interruptions
NSURL *url = [NSURL URLWithString:link];
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:url cachePolicy:0 timeoutInterval:kTimeoutInterval];
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
UIImage *image = [[UIImage alloc] initWithData:self.activeDownload];
// set to ImageView
cell.ThumbImage.image = image;
}];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"TableViewCell" forIndexPath:indexPath];
cell.tag = indexPath.row;
cell.imageView.image = nil;
// Rounded Rect for cell image
CALayer *cellImageLayer = cell.imageView.layer;
[cellImageLayer setCornerRadius:35];
[cellImageLayer setMasksToBounds:YES];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(queue, ^(void) {
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:_ResimAdi[indexPath.row]]];
UIImage *image = [[UIImage alloc] initWithData:data];
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
if (cell.tag == indexPath.row) {
CGSize itemSize = CGSizeMake(70, 70);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[image drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[cell setNeedsLayout];
}
});
}
});
cell.TitleLabel.text = _TarifAdi[indexPath.row];
return cell;
}
How can i check web service with database if database have will show image in database but not its will show image from web service
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
BookCell *bookcell = [collectionView dequeueReusableCellWithReuseIdentifier:#"ReuseCell" forIndexPath:indexPath];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *imageURL = [NSURL URLWithString:[[bookList objectAtIndex:indexPath.item]coverURL]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSString *bookPath2 = [NSString stringWithFormat:#"%#/assets/book/%#/cover.png",path,bookPath];
NSLog(#"bookpath2 = %#",bookPath2);
dispatch_async(dispatch_get_main_queue(), ^{
if ([db isDownloaded:bookName bookVersion:[bookVersion floatValue]]) {
[bookcell.bookCover setImage:image];
[bookcell.bookCover setAlpha:1.0f];
// or this [bookcell.bookCover setImage:[UIImage imageWithContentsOfFile:bookPath2]];
}
else{
[bookcell.bookCover setImage:image];
[bookcell.bookCover setAlpha:0.5f];
[bookcell.bookDetailLabel setHidden:NO];
[bookcell.bookDetailLabel setAlpha:1.0f];
}
});
});
return bookcell;
}
Advice me Please.
Help me Please.
I'm try this is work for me,Thanks! for any Idea
if (bookPath2 = [NSString stringWithFormat:#"%#/assets/book/%#/cover.png",path],[[bookList objectAtIndex:indexPath.item] bookPath] && bookPath2 != [NSString stringWithFormat:#"%#/assets/book/(null)/cover.png",path]) {
NSLog(#"++++++++++++++++++++++");
bookcell.bookCover.image = [UIImage imageWithContentsOfFile:bookPath2];
if (checkversion < checkNewversion) {
bookcell.bookDetailLabel.text = #"Update";
bookcell.bookDetailLabel.hidden = NO;
} else
{
bookcell.bookDetailLabel.hidden = YES;
}
}
else {
bookcell.bookCover.image = nil;
NSLog(#"-------------------------");
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *imageURL = [NSURL URLWithString:[[bookList objectAtIndex:indexPath.item]coverURL]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
dispatch_async(dispatch_get_main_queue(), ^{
bookcell.bookDetailLabel.text = #"New";
bookcell.bookCover.image = [UIImage imageWithData:imageData];
bookcell.bookCover.alpha = 0.5f;
bookcell.bookDetailLabel.hidden = NO;
});
});
}
return bookcell;