I use SDWebImage to download images into my UITableView using:
[cell.imageView setImageWithURL:[NSURL URLWithString:tempPhotoURL] placeholderImage:[UIImage imageNamed:#"temp.jpg"]];
This works perfectly - no problem there. But when I click on any given row in my TableView, I want to load that row's thumbnail-image into a UIImageView that's sitting in the oncoming detail-screen to which I'm navigating. Well how do I do this? Do I now have to get the image from the cache since its already been downloaded? If so, what's the method/process for this? And if that's not the way to go, what is?
I can't figure it out from all those files and methods included in the SDWebImage library...
Simply use almost exactly same code to set the image - SDWebImage will check if it is already cached and if so, it will use it instead of fetching image from the url.
Try this,
[self.imgView sd_setImageWithURL:[NSURL URLWithString:imageUrl placeholderImage:placeholderUrl options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
//Progress block
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL){
//Completion block
}];
There are many ways to achieve things when coding.
One that springs to mind is to ensure the UIImageView is an accessible property of your detail view controller (part of the detail view controller's API).
Then all you need to do is before you push your detail view controller, assign the cell's image view to the detail view's UIImageView property.
OR
create a convenience initialiser and pass the cell's imageview to it...
etc etc.
Call the same method, you downloaded with in your TableView in DetailView when you set the picture, it will check the cache first. Here's the code snippet to download image with SDWebImage
[yourImageView sd_setImageWithURL:[NSURL URLWithString:yourImageUrl]
placeholderImage:[UIImage imageNamed:#"placeholder.jpg"]];
Related
I am using ASIHTTPRequest. I have the following issues while using ASIHTTPRequest.
1.) I need to add images to UITableView (for each cell) asynchronously. How can i do this ?
2.) I need to add an image to a UIViewController Asynchronously. (Not to a cell, but just on the UIImageView, which is on a UIViewController).
Can someone please help me with some sample code, Example or a Tutorial to start with?
No need to introduce a dependency to a whole framework such as ASIHTTPRequest just to download one image, when you can do it a few easy lines of code using GCD:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imageDate = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
dispatch_async(dispatch_get_main_queue(), ^{
avatar.image = image;
});
});
This is asynchronous and all the goodness. But in a few lines of code you can write, understand, bug-fix, extend and maintain yourself.
But in case you are bent on using ASIHTTPRequest I suggest using this excellent project Here is a sample code to have a guide line and a brief description.
One other way is that you can use the asynchronous image view instead of the default image view. check tutorial Here and also How? UITableViewCell with UIImageView asynchronously loaded via ASINetworkQueue
Basically (see example page with all the correct syntax) what you do is
Create a request.
Set yourself as a delegate for when it's complete.
Start the request.
In -(void)requestFinished: you add the image to your TableView as you would in normal code.
I'm not sure that it's exactly what you are looking for, but check out the lazy image loading example from apple. But they use NSURLConnection.
Hope it helps
I am using SDWebImage in my iPhone project and I use it to load images to my table cell.
The original downloaded image 150 * 150.
My placeholder image is 32*32 (required size)
First time, when the image loads, sdwebimage does a great job of re-sizing the downloaded image to match the size of the placeholder image (32*32). All good.
However, when I navigate back and then come back to the same page, the image gets stretched and fills the entire cell.imageView height. Is this normal behavior?
I need it to always retain the initial 32*32 size. Can you suggest how to do that?
EDIT: I found this link where another user faces a similar issue (question open). The user also made a short youtube video to explain the issue.
Thoughts?
try that for specific size at the cellForRowAtIndexPath :
UIImageView *addImage = [[UIImageView alloc] init];
[addImage setImageWithURL:[NSURL URLWithString:yourCellImageURL] placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
[addImage setFrame:CGRectMake(7, 3, 78, 57)];
[cell.contentView addSubview:addImage];
[cell setIndentationLevel:8];
tldr: If your custom UITableViewCell subclass has a #property IBOutlet UIImageView* called imageView, change your name to something else (such as customImageView) and it will work properly. Read the rest of the answer for why
I see nobody has answered that, and I wasted a whole week trying to find the solution to this, so here's what happened to me, and how to prevent it happening to you (thus fixing your problem).
I created a CustomTableViewCell which extends UITableViewCell.
I added #property (weak, nonatomic) IBOutlet UIImageView *imageView; to my custom cell.
Everything seems fine until now, right?
Except it is not.
UITableViewCell already has an imageView property. iOS 7 uses your property at first, but then when the Cell is being reused, the UITableViewCell's imageView is used, therefore ignoring all of your constraints or previously defined fixed sizes.
iOS 8 always uses the image from UITableViewCell. so the problem is more obvious there. Previous iOS versions also had varying degrees of this problem, usually presenting very similar symptoms.
The solution? Either deal with the property UITableViewCell.imageView by yourself (add code for laying out the view) or create your own property with a different name, with which you can add constraints (in case you're using auto layout) on Storyboards or xib.
Try to set proper content mode of the UIImageView
if you want to change image size in SDWebImage.
use this call.
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) { /*here you can change size of image */ }];
I am loading image with data in my table view. Images are coming from web. I created method to get image url in model class.Model class has Nsdictionary and objects.
But this images is slowing down scrolling .Here is my code
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",
[(Tweet *)[recentTweets objectAtIndex:indexPath.row]urlString]]]]];
cell.imageView.image = image;
Please tell Where I am going wrong?
Use lazy loading and do your own drawing. Try to understand the techniques on the sample projects I linked. These are the best ways to improve the performance of tables with images.
here is the methodology I use for loading images into a UITableView from a remote location:
in your .h file, declare a mutable dictionary for storing the images:
NSMutableDictionary *images;
initialize the dictionary in -init... or in -viewDidLoad
images = [[NSMutableDictionary alloc]init];
in the .m, tableView:cellForRowAtIndexPath:, see if the image exists in your dictionary for the indexPath
UIImage *img = [images objectForKey: indexPath];
if the image does exist, just set it.
if (img) cell.imageView.image = img;
if the image does NOT exist, set the cell's image to a temporary image...
if (!img) cell.imageView.image = [UIImage imageNamed:#"imageUnavailable.png"];
AND add that image to your dictionary so it doesnt try to refetch the image if you scroll off and back to that image before it loads...
[images setObject:[UIImage imageNamed:#"imageUnvailable.png"] forKey: indexPath];
then, in this same block, use an NSOperationQueue, and a custom NSOperation ( here is a reference - NSOperation and SetImage) to get your image and call back into your UITableViewController with that image.
in your callback, add your image to the dictionary (overwriting the temp image) and call [tableView reloadData]
this will give you a nice non blocking user experience.
The are a couple of ways how to do it. I had the best experience with a Queue for the HttpRequests, which I pause during the scrolling process.
I highly recommend this framework for that:
http://allseeing-i.com/ASIHTTPRequest/
I also implemented an image cache, which only loads the images if there weren't in the cache.
And the last tweak was to actually draw the images instead of using a high level uicomponent like the UIImageView
The number one reason your code is slow right now is because you're making a network call on the main thread. This blocks an UI from being updated and prevents the OS from handling events (such as taps).
As already suggested, I recommend ASIHTTPRequest. Combine asynchronous requests with an NSOperationQueue with a smaller concurrency count (say, 2) for the image requests, caching, and reloading the rows when images come in (on the main thread) (also only reloading the row if its currently visible). You can get a smooth scrolling table view.
I have an example of this on github here: https://github.com/subdigital/iphonedevcon-boston
It's in the Effective Network Programming project and includes a set of projects that progressively improve the experience.
Download the images before you load the tableView, and store them in an NSArray. Then when the cellForRowAtIndexPath method is called, show a loading image until the image is in the array.
How can I pass an image that is in my UITableViewCell to my TableDetailsView.
I tried setting the data on Post, which is my type of data that fills the UITableView cells.
[[cell avatar] setImage:[UIImage imageNamed:#"butterfly.jpeg"]];
[post setImage: [UIImage imageNamed:#"butterfly.jpeg"]]; <-- app crashes here.
All it seems that Question still is not complete....
But looking at two lines of code & you want to pass an image then you could do this thing in lots of ways.
so for testing purpose I will suggest ,just use a Global String i.e in Delegate.
like.Otherwise can you can use this link for Global Variable
Pass a variable between lots of UIVIewControllers
DelegateClassObj.yourGlobalString = [NSString stringwithFormat:#"%#",#"butterfly.jpeg"];
from the class where you want to pass.
&
now if you want to use it:
Suppose post in your imageView then
[post setImage: [UIImage imageNamed:DelegateClassObj.yourGlobalString]];
Hope this will work....the easir way :)
If Your motive is to Display same Image on detail view, then you can pass the name (or Id) of that image on cell tap. and load that image in your detail view through that.
I need a clarification from all of you,That is I am implementing an iPad application. In that I tried to download and animate the images. The image count should be more than 100,000.The code I used to download and adding to the view is as follows.
UIImageView* imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,100,100)];
NSData *receivedData=nil;
receivedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://path/prudently/iphone/image_s/e545afbf-4e3e-442e-92f9-a7891fc3ea9f/test.png"]];
imageView.image = [[UIImage alloc] initWithData:receivedData] ;
[subView addSubview:imageView];
[imageView release];
But I am getting exception after I successfully added more than 8000 image to my subview. I am getting exception at getting data from the url. And one more thing I am not releasing the subview because once I downloaded them I need to animate the subview.
Please give me your suggessions
Thank you,
Sekhar Bethalam.
100,000 images would seem a lot for desktop applications, let alone a smart phone like an iPhone. Is there not another approach you can take to solve this problem that wouldn't need such a high resource count?
You can write the URL , images or something to a cached file, and divide some pages to animate the images...
When the user press a page link , application read and animate the images of this page, images of the page which user don't use need not display.
The only way you are going to accomplish this is to dynamically create and destroy the UIImageViews as they are needed on the screen. The iPhone/iPad/iAnything are incapable of doing what you want because of the limited memory available on the device.