in the UITableView that have some cells, and each cell have tow UIImage, and i will update the UIImage asynch for performance. but there is a question, how can i only reload the UIImageView in cell when a image pulled from the Web.
i kown can reload a whole cell with methoed "reloadRowsAtIndexPaths", but i just want to kown is there have a method can only reload the UIView object which is necessary to be reload (i.e. a image or a label) in the cell.
BTW, i found the methoed "reloadRowsAtIndexPaths" will execute the method "heightForRowAtIndexPath" for all cells when i just reload one cell. is there anything wrong?
Maybe the sample code LazyTableImages from apple is what you want.
Get a reference to your custom cell calling tableView:cellForRowAtIndexPath:. Then update its image property running this:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSData *imageData = [NSData dataWithContentsOfURL:someURL];
dispatch_async(dispatch_get_main_queue(), ^{
cell.someImage = [UIImage imageWithData:imageData];
});
});
You'll be loading at most 6 images concurrently because that's the limit of NSURLConnection.
Related
I used the code below for years to be able to capture a screenshot of an UITableView (including hidden rows) and save it to the user's phone gallery or share it.
Since they updated to iOS 13 it doesn't work anymore, it captures only the visible part of the table leaving it blank on the bottom part.
-(UIImage *)imageFromCurrentTable
{
CGRect frame = self.tableView.frame;
frame.size.height = self.tableView.contentSize.height;
self.tableView.frame = frame;
UIGraphicsBeginImageContext(self.tableView.bounds.size);
[self.tableView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
return [UIImage imageWithData:data];
}
What changed in iOS 13? How this code can be updated? (the code is Obj-C but I will accept also swift answers!)
I have faced this issue. Due to cell Reuse, UIGraphicsGetImageFromCurrentImageContext cannot produce full UITableView structure.
One Way [Not Efficient Way]:
In cellForRowAtIndexPath, we can able to get which UITableViewCell using. Store that cell in [Int: UITableViewCell].
Get screenshot from UITableViewCell.contentView.
Add that screenshot's image as subview to UIView one by one.
Now, UIView having UITableView's contentView as Images.
Get screenshot from UIView.
Am having UITableView and i need to list my saved images from document directory am using the code NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
dispatch_queue_t queue_=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue_, ^{
NSString *filePath=[docDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"MyFolder_New/%#",[[[tableArray objectAtIndex:indexPath.row] componentsSeparatedByString:#"/"] lastObject]]];
dispatch_async(dispatch_get_main_queue(), ^{
[img setImage:[UIImage imageWithData:[NSData dataWithContentsOfFile:filePath]]];
[indicator stopAnimating];
});
}); for displaying images Asynchronously into UITableView but the UITableView is lagging while scrolling to top and bottom.
You need use dispatch_asynch in method tableView:cellForRowAtIndexPath: when you customize cell before return.
Steps:
1. get cell from pool.
2. customise it.
3. start dispatch_asynch, what will set correct image (this need check if cell is still same) to cell.
We do it all time and we never have lags. But images appear in cells with delay (it is correct). Try it.
I need to load image from server(url) to UITableView. So i tired below code, but no use.
UIImageView *img=[[UIImageView alloc]init];
img.frame=fr;
img.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:countimg]]];
[cell.contentView addSubview:img];
Here countimg is NSMutableArray. countimg contains url of all images to load. But its not working, because its not a string variable. I don't know how to change NSMutuableArray to String.
Anyone help me.
You can get string urls from countimg array like this,
[countimg objectAtIndex:index];
Where index is an integer of range between >=0 && < [countimg count]
Now if you're using countimg array in UITableView delegate then, you've replace index variable with indexPath.row.
So for now your code would look like this,
UIImageView *img=[[UIImageView alloc]init];
img.frame=fr;
img.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[countimg objectAtIndex:indexPath.row]]]]; //note this
[cell.contentView addSubview:img];
Update:
As #DavidAtkinson, suggestion, you should not use dataWithContentsOfURL method to load a NSURL to download a image. As its perform in main thread and will stuck the UI until download would not completes. Its better to load image in background.
Also, there's some asynchronous UIImageView's available, one which I am using is SDWebImage.
Use SDWebImage to load your image from server. here using this library you can put placeholder image in tableview cell's image view till the image loaded. You can even cache your image by just doing a single line code
[UIImageView setImageWithUrl:[pass your URL here] placeholderImage:[pass name of placeholder image] options:SDWebImageRefreshCached]
If you are trying to load your images from URL into a tableview,the SDWebImage code from Github does not specify how to do that.You have to create an object at index,then load the object for key name from the file you are parsing from.This will require you to change the SDWebImage code so that instead of parsing directly from a specific url,its parsing from
an object key name that contains an image url from your json or xml file.
Here is a sample code for loading an image from an object key in a json:
NSURL* url = [NSURL URLWithString:[dictionaryObject valueForKey:#"yourfile"]];
NSData *data = [NSData dataWithContentsOfURL:url];
dispatch_sync(dispatch_get_main_queue(), ^{
UIImageView *imgViewThumb=[[UIImageView alloc]initWithFrame:imgView.frame];
[imgViewThumb setImage:[UIImage imageWithData:data]];
[cell addSubview:imgViewThumb];
This code is in in a cell initialization routine that sets up the elements of a custom cell. It gets the image from the web asynchronously. But I need it to redraw once it's done.
This is my snippet of code:
dispatch_async(myCustomQueue, ^{
//Look for the image in a repository, if it's not there
//load the image from the web (a slow process) and return it
mCover.image = [helperMethods imageManagerRequest:URL];
//Set the image to be redrawn in the next draw cycle
dispatch_async(dispatch_get_main_queue(), ^{
[mCover setNeedsDisplay];
});
});
But it doesn't redraw the UIImageView. I've tried to also redraw the entire cell, and that doesn't work either. Your help is much appreciated. I've been trying to fix this for some time!
Instead of setNeedsDisplay, you should set the image on main thread as Apple have mentioned in their documentation.
Note: For the most part, UIKit classes should be used only from an
application’s main thread. This is particularly true for classes
derived from UIResponder or that involve manipulating your
application’s user interface in any way.
This should fix your problem:
dispatch_async(myCustomQueue, ^{
//Look for the image in a repository, if it's not there
//load the image from the web (a slow process) and return it
UIImage *image = [helperMethods imageManagerRequest:URL];
//Set the image to be redrawn in the next draw cycle
dispatch_async(dispatch_get_main_queue(), ^{
mCover.image = image;
});
});
Currently, I'm working on the client for the website. I have tonne of images that I need to load to my TableView. Here is what I'm currectly doing:
NSDictionary *propertyItems = [self.items objectAtIndex:indexPath.row];
cell.fio.font = [UIFont boldSystemFontOfSize:17.0f];
if([propertyItems objectForKey:#"online"] == [NSNumber numberWithInt:1])
cell.fio.textColor = [UIColor colorWithRed:0.3 green:0.6 blue:0.3 alpha:1.0];
dispatch_queue_t downloadPhotosQueue = dispatch_queue_create("downloadPhotosQeue", NULL);
dispatch_async(downloadPhotosQueue, ^{
NSData *photosData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.example.com/%#", [propertyItems objectForKey:#"photo_trumb"]]]];
dispatch_async(dispatch_get_main_queue(), ^{
cell.pic.image = [UIImage imageWithData:photosData];
});
});
cell.fio.text = [propertyItems objectForKey:#"fio"];
I'm doing this in cellForRowAtIndexPath: method.
Everything is loading fast. But the problem is, when I'm scrolling my table down, and after again up, the images are reloading over and over again.
Question: Is there any way to easily cache my UIImages that I'm getting from the server? So if they are loaded once, they wouldn't reload over and over again, while I'm running the app. Maybe I'm doing something wrong?
Thanks in advance!
I strongly recommend AsyncImageView. I use it and it works like a charm. Just set the image url and it handles everything itself.
AsyncImageView *imageView = [[AsyncImageView alloc]init];
imageView.imageURL = [NSURL URLWithString:#"https://www.google.es/logos/classicplus.png"];
It caches the image in memory so it won't retrieve it from the server again. It will also release them when receiving a memory warning.
To load image from a website I use AFNetworking. It provides a class to load an image from an URL:
Start by adding #import "UIImageView+AFNetworking" to the top of a
controller or a view implementation file. This category adds methods
to UIImageView, like:
[imageView setImageWithURL:[NSURL URLWithString:#"…"]];
i recommend this library to accomplish what you want.
in cellForRowAtIndexPath:
[cell.imageView setImageWithURL:
[NSURL URLWithString:[NSString stringWithString:#"www.yourimagepath.path"]]
placeholderImage:[UIImage imageNamed:#"no_image.jpg"]];
the library will cache the image for you. you can also set the setHolder of the image, so it wont look like a blank image while the images are downloading.
The problem is here :
NSData *photosData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.example.com/%#", [propertyItems objectForKey:#"photo_trumb"]]]];
dataWithContentsOfURL
It launch a request to the Url everytime the function is called (everytime you scrolled).
You need to load all the picture before cellForRowAtIndexPath.
Like in ViewDidLoad.
You can just store them in an array and display your picture's array in cellForRowAtIndexPath.
If it's really fast to load like you say : jsut load picture Once in CellForRowAtIndexPath. Store them in a mutableArray. And check if the picture already exist .