SDWebImage在缓存之前调整大小/缩放/裁剪来自url的图像

时间:2012-08-31 07:03:13

标签: objective-c ios5 sdwebimage

如何在缓存之前调整来自网址的图像的大小,缩放和裁剪。我尝试使用以下代码实现此目的。

[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];

这产生了一个奇怪的结果。首先,它显示未调整大小/缩放/裁剪的版本。但是,当我向下滚动然后返回时,它会显示调整大小/缩放/裁剪的版本。我知道这段代码首先缓存图像并调整大小/缩放/裁剪它们。但是,我无法想出更好的解决方案。我认为应该有一些能够下载图像的东西,以便独立调整大小/缩放/裁剪和缓存。有什么想法吗?

谢谢。

4 个答案:

答案 0 :(得分:13)

我自己想出了解决方案。

而不是

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

自动下载和缓存图像,使用此

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];

不要忘记包含

#import <SDWebImage/UIImageView+WebCache.h>
@interface YourViewController : UIViewController <SDWebImageManagerDelegate>

答案 1 :(得分:4)

如果您想扩展 - (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

DECLARE @LineBreak VARCHAR(2)=CHAR(13)+CHAR(10); --might be only 10 or only 13... DECLARE @yourString VARCHAR(MAX)= 'abc def $ ghi & jkl'; SELECT The.Parts.value('.','varchar(max)') FROM (SELECT CAST('<x>' + REPLACE(REPLACE(REPLACE(REPLACE(@yourString,'<','&lt;'),'>','&gt;'),'&','&amp;'),@LineBreak,'</x><x>') + '</x>' AS XML)) AS MyString(AsXml) CROSS APPLY MyString.AsXml.nodes('x') AS The(Parts) WHERE LEN(The.Parts.value('.','varchar(max)'))>1 方法来源:The simplest way to resize an UIImage?

答案 2 :(得分:2)

这是如何使用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];

答案 3 :(得分:0)

如果有人在寻找swift-3版本

    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!

    }

`