10

How can I resize, scale and crop images coming from url before caching them. I tried to achieve this with the following code.

[cell.profilePicture setImageWithURL:[NSURL URLWithString:[rowData objectForKey:@"pictureUrl"]] placeholderImage:nil];
cell.profilePicture.image = [self imageByScalingAndCroppingForSize:CGSizeMake(cell.profilePicture.frame.size.width, cell.profilePicture.frame.size.height) image:cell.profilePicture.image];

This produced a strange result. First, it shows a not resized/scaled/cropped version. But, when I scroll down and then go back, it shows a resized/scaled/cropped version. I know that this code first caches images and resizes/scales/crops them. But, I could not come up with a better solution. I think there should be something that enables to do downloading images so as to resize/scale/crop and caching independently. Any ideas?

Thank you.

Sukhrob
  • 901
  • 4
  • 12
  • 34

4 Answers4

13

I figured out the solution on my own.

Instead of

[cell.profilePicture setImageWithURL:[NSURL URLWithString:[rowData objectForKey:@"pictureUrl"]] placeholderImage:nil];

which downloads and caches images automatically, use this

SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:url delegate:self options:0 success:^(UIImage *image) 
{
    cell.profilePicture.image = [self imageByScalingAndCroppingForSize:CGSizeMake(cell.profilePicture.frame.size.width, cell.profilePicture.frame.size.height) image:image];
} failure:nil];

Don't forget to include

#import <SDWebImage/UIImageView+WebCache.h>
@interface YourViewController : UIViewController <SDWebImageManagerDelegate>
Sukhrob
  • 901
  • 4
  • 12
  • 34
  • Sukhrob, I have added an alternative to this answer. I modified SDWebImage in order to do this. – Tony Oct 28 '12 at 02:31
  • 11
    This does not resize before caching. It resizes **every time** the image is fetched, either downloaded or from cache. The completion block is called **after** caching. That does not answer your question. [This does.](http://stackoverflow.com/questions/12928103/sdwebimage-process-images-before-caching) – Eran Goldin Jul 28 '15 at 15:05
4

If you want to scale UIImage, try this way:

 - (void)viewDidLoad {
   [super viewDidLoad];

        SDWebImageManager *manager = [SDWebImageManager sharedManager];
        [manager downloadImageWithURL:[NSURL URLWithString:yourPhotoURL]
                              options:SDWebImageRefreshCached
                             progress:nil
                            completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                CGSize size = CGSizeMake(50,50);//width,height
                imageView.image = [self imageWithImage:image scaledToSize:size];
            }];
    }
- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize{
     UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
     [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
     return newImage;
}

imageWithImage method source:The simplest way to resize an UIImage?

Community
  • 1
  • 1
Gent
  • 6,215
  • 1
  • 37
  • 40
2

This was a great example on how to implement in using SDWebImageManagerDelegate.

#import <Foundation/Foundation.h>
#import <SDWebImage/SDWebImageManager.h>

@interface ImageLoader : NSObject <SDWebImageManagerDelegate>
+ (instancetype)sharedImageLoader;
@end

@implementation ImageLoader

+ (instancetype)sharedImageLoader
{
    static ImageLoader* loader = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        loader = [[ImageLoader alloc] init];
    });

    return loader;
}

- (UIImage *)imageManager:(SDWebImageManager *)imageManager
 transformDownloadedImage:(UIImage *)image
              withURL:(NSURL *)imageURL
{
    // Place your image size here
    CGFloat width = 50.0f;
    CGFloat height = 50.0f;
    CGSize imageSize = CGSizeMake(width, height);

    UIGraphicsBeginImageContext(imageSize);
    [image drawInRect:CGRectMake(0, 0, width, height)];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

@end

Then just add the delegate where needed.

[SDWebImageManager sharedManager].delegate = [ImageLoader sharedImageLoader];
Dmytro Rostopira
  • 10,588
  • 4
  • 64
  • 86
Alberto Lopez
  • 247
  • 4
  • 10
0

If anyone is looking for swift-3 version

    self.postImage.sd_setImage(with: URL(string: image)!, placeholderImage: UIImage(named: "image_placeholder"),options: SDWebImageOptions(rawValue: 0),
      completed: { (image, error, cacheType, imageURL) in
         //Scale or do what ever you want to do to the image          
         let newImage = self.scaleImage(image: image!, view: self.postImage)
         self.postImage.image = newImage
     })


func scaleImage(image:UIImage, view:UIImageView) -> UIImage {

        let oldWidth = image.size.width
        let oldHeight = image.size.height

        var scaleFactor:CGFloat
        var newHeight:CGFloat
        var newWidth:CGFloat
        let viewWidth:CGFloat = view.width

        if oldWidth > oldHeight {

            scaleFactor = oldHeight/oldWidth
            newHeight = viewWidth * scaleFactor
            newWidth = viewWidth
        } else {

            scaleFactor = oldHeight/oldWidth
            newHeight = viewWidth * scaleFactor
            newWidth = viewWidth
        }

        UIGraphicsBeginImageContext(CGSize(width:newWidth, height:newHeight))
        image.draw(in: CGRect(x:0, y:0, width:newWidth, height:newHeight))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage!

    }

`

Chrishan
  • 4,076
  • 7
  • 48
  • 67