Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImage+RoundedCorner with SDWebImage

I am using SDWebImage to load images into my table view. I would like the images in my table view to be resized and have round corners. I found UIImage+Resize and UIImage+RoundedCorner to do this. Both libraries works great seperately but I have not been abl to combine them. I could resize and round the corners of the image SDWebImage returns but I have found this to be fairly resource heavy and therefore I would like to have the images resized before saving them to the cache. When an image is loaded from the net for the first time it is probably shown before being saved to the cache therefore I would also like to resize the image when it is loaded for the first time.

I have not been able to do this as I cannot figure out which method of SDWebImage to manipulate the image in. All I need is to call the following on the right UIImage in SDWebImage.

UIImage *image = [image thumbnailImage:50 transparentBorder:0 cornerRadius:5 interpolationQuality:kCGInterpolationHigh];

Can anyone tell me where in SDWebImage I should place this piece of code to have the image manipulated before being saved to the cache and have a manipulated image sent to the image view when it is loaded from the internet and not the cache?

like image 802
simonbs Avatar asked Aug 13 '11 18:08

simonbs


2 Answers

For the resize image part, I don't have a good answer.

For the round corners feature, you are completely going to the wrong way, this is what I try to do before... try to resize and round every image and save it to the disk...... too complex, too many things to do...

the correct and simple way is to set the UIImageView cornerRadius of your table cell:

cell.imageView.layer.cornerRadius = 8;
cell.imageView.layer.masksToBounds = YES;
like image 136
flypig Avatar answered Oct 01 '22 15:10

flypig


I figured this out.

You should manipulate the image in the following methods in SDImageCache.m:

 1. - (void)storeImage:(UIImage *)image forKey:(NSString *)key;
 2. - (void)storeImage:(UIImage *)image forKey:(NSString *)key toDisk:(BOOL)toDisk;
 3. - (void)storeImage:(UIImage *)image imageData:(NSData *)data forKey:(NSString *)key toDisk:(BOOL)toDisk;

In the third method you must remember to convert your UIImage to NSData using the code below. This should be done if toDisk is true.

NSData *croppedRoundedImageData = UIImageJPEGRepresentation(croppedRoundedImage, 1.0);
if (croppedRoundedImageData) data = croppedRoundedImageData;

Using if (croppedRoundedImageData) data = croppedRoundedImageData; your app will not crash if your data is NULL when you try to save it.

In SDWebImageDownloader.m you must add your code for manipulating in - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection This is used for the first load of the image (when not in cache)

like image 20
simonbs Avatar answered Oct 01 '22 13:10

simonbs