Get size of image loading through URL - ios

In my application I am displaying feed in tableview. Image is coming through URL. I want to calculate cell height dynamically based on image size. But I get image after cell get load.
How to achieve dynamic height of cell based on image size?
I hope my question is clear and I will get solution here.
Thanks,

When your images are loaded, you can calculate all heights for your rows, and if your heightForRowAtIndexPath method is implemented respectively just call [tableView reloadTable].

The simplest way would be to get the image sizes in the api itself if the service is handled by you itself. Since without getting the image downloaded you will not get its size.
Other way would be to keep the imageview in table row in a constant size and aspect fill the image inside it as most of the applications do.

Try this code:
But you can use image downloading dispatch_async.
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://static.hugedomains.com/images/logo_huge_domains.gif"]];
UIImage *image = [UIImage imageWithData:imageData];
NSLog(#"image height: %f",image.size.height);
NSLog(#"image width: %f",image.size.width);

Related

How to pull old UIImage from cache with SDWebImage and put it to placeholder?

I have 2 lists of images with 2 different size.
One is thumbnail images (already cached by SDWebImage).
One is original images, these images will be loaded when user tap to view detail cell.
The question is I want to put thumbnail image as a placeholder with SDWebImage.
There is a function to do it:
cell.image.sd_setImageWithURL(NSURL(string:orgimgurl!), placeholderImage: UIImage())
However, how to pull and set UIImage as a placeholderImage from cache it its exist?
One way is that you can pass the thumbnail image to the detailView and then use it in the parameter when loading original image.
Or Try
NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:url];
UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key];

How would I design an asynchronous image downloader for my UITableView that prioritizes downloads depending on location in table view?

In my app, I receive a list of image URLs to use as thumbnails in my table view. The tableview has a small amount of items, approximately 30, so I want to load all the thumbnails immediately (instead of when they become visible, as they undoubtedly will become visible and I want them fully loaded then).
I want to prioritize the image downloads by index path, so the first index path has priority over the second, which has priority over the third, etc.
However, if I suddenly jump to the end of the table view (which shows, say, index paths 24-29) I want the images for those index paths to become highest priority, so if they jump at the very beginning they won't have to wait for all the others to download first.
How would I go about designing something like this? If it's possible with SDWebImage that'd be great, as I'm comfortable with that, but if not I'm more than fine with creating something from scratch with NSURLSession. (I've looked at SDWebImage, and the SDWebImagePrefetcher looks promising, but doesn't allow priority changing from what I've seen.)
I recently had a similar problem, but didn't need to have a priority to load. I can't think of how to change the priority of what is loaded unless you do the loading of thumbnails before loading the tableview. Tableviews load cells as they are needed.
I can think of two options:
If you want all the data loaded and ready before the tableview is even loaded, preemptively load the data in an earlier view controller and save it to be opened in the view controller containing your table view. Then no requests will have to be made by your tableview and everything will appear seamlessly.
The tableview sends each thumbnail request asynchronously and updates the cell as the images arrive on the main thread. You can then cache or save the images after they arrive. But the images won't appear instantly, generally a split second later.
I did option two using NSCache. The value for key "avatar" is the URL of the thumbnail image. This code is located in my - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
if (![teamMember[#"avatar"] isEqualToString:#""]) {
// check for cached image, use if it exists
UIImage *cachedImage = [self.imageCache objectForKey:teamMember[#"avatar"]];
if (cachedImage) {
cell.memberImage.image = cachedImage;
}
//else retrieve the image from server
else {
NSURL *imageURL = [NSURL URLWithString:teamMember[#"avatar"]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
// if valid data, create UIImage
if (imageData) {
UIImage *image = [UIImage imageWithData:imageData];
// if valid image, update in tableview asynch
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
TeamCommitsCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath];
// if valid cell, display image and add to cache
if (updateCell) {
updateCell.memberImage.image = image;
[self.imageCache setObject:image forKey:teamMember[#"avatar"]];
}
});
}
}
});
}
}

how to load two images with Lazyloading in one cell

In my application I have used Lazyloading for displaying image from url. But now i want to display two multiple images in one cell. How can i do this please help me.
Maybe this will help you. add this code in cellfor row and for Download Image use SDWebImage
UIImageView *imgView1=[[UIImageView alloc]init];
imgView1.frame=CGRectMake(0,0,20,20);//Your Frame
imgView1.image = [UIImage ImageNamed:#"a"]; // your image or download image code or use Sdwebimage
[cell.contentView addSubview:imgView1];
UIImageView *imgView2=[[UIImageView alloc]init];
imgView2.frame=CGRectMake(25,0,25,25);//Your Frame
imgView2.image = [UIImage ImageNamed:#"b"]; // your image or download image code or use Sdwebimage
[cell.contentView addSubview:imgView2];
If you want to add more images then add same code and just give frame.

UIImageView set to imageWithData ignores contentMode

In my custom tableViewCell, I have an imageView (not the tableViewCell's default imageView) which fills the entire cell. It is set up in IB (and by code) with the contentMode UIViewContentModeAspectFill, and it clips to bounds. Whenever I change the imageView's image to an image loaded with [UIImage imageNamed:...], it resizes and fits the imageView as wished and expected. However, when the image set is loaded with [UIImage imageWithData:...], the image is set, but not resized.
The code that loads the image with data itself is run in a background thread, and looks like this:
- (void)getImage:(NSString *)URL {
NSError *error = nil;
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:URL] options:NSDataReadingMapped error:&error]];
if (!error){
[self.thumbView setImage:image];
[self.thumbView setContentMode:UIViewContentModeScaleAspectFill];
}
}
I have tried setting the image on the main thread, in layoutSubviews, but it always produces the same result – it doesn't resize to fill the imageView. I added the image (JPG) to the app bundle and set it programmatically with [UIImage imageNamed:...], which works, but the app has to get the data from a URL.
I also tried using the UIImageView+AFNetworking-class to set the image async instead of making my own thread, but that doesn't work either.
Why isn't the image respecting the UIImageView's contentMode when it's loaded from data? Any help is appreciated. Neither JPEGs nor PNGs are working.
If anyone else stumbles upon the same issue, here's what I did to fix it.
I made a new imageView in IB, removed the old one, and only changed the contentMode-value and clipsToBounds-value. In the tableViewCell.m init-code, I set the placeholder image, and voila – everything works like a charm.
Although I don't really know what caused it, it may have been the Clears Graphics Content checkmark's fault for being set (oops).

How to truncate a UIImage in iOS

How can I truncate the left side of an image stored in a UIImage object. Basically in certain situations I just want to show part of an image.
How can I do this on with the iOS sdk?
P.S. I tried changing the frame size of the UIImage but that just scales the image and distorts it.
A very simple way is to load the image into a UIImageView, and then add the view to another view. You can then position the image view so that its .frame.origin.x property is negative, which will place it off to the left. The parent view needs to have setMasksToBounds:YES called on it, or else the image view will still be fully-visible.
There are many other ways to achieve this effect as well, but this may be the simplest for you to implement.
to crop a UIImage, you can use one of the UIImage categories available out there, such as http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/
For example, this frame will remove 100 pixel from the left side of a 200x200 pixel UIImage
CGRect clippedRect = CGRectMake(100, 0, 100, 200);
UIImage *cropped = [self imageByCropping:lightsOnImage toRect:clippedRect];

Resources