Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDWebImage resizing/scaling/cropping images coming from url before caching them

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.

like image 836
Sukhrob Avatar asked Aug 31 '12 07:08

Sukhrob


3 Answers

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>
like image 178
Sukhrob Avatar answered Sep 29 '22 10:09

Sukhrob


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?

like image 35
Gent Avatar answered Sep 29 '22 09:09

Gent


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];
like image 33
Alberto Lopez Avatar answered Sep 29 '22 10:09

Alberto Lopez