View Full Version : UIImage+RoundedCorner with SDWebImage

Aug 15, 2011, 02:25 AM

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?

Aug 15, 2011, 03:31 AM
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 *)aConnectionThis is used for the first load of the image (when not in cache)