10

如何在缓存来自 url 的图像之前调整大小、缩放和裁剪图像。我试图用下面的代码来实现这一点。

[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

4 回答 4

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>
于 2012-09-02T13:56:53.287 回答
4

如果要缩放 UIImage,请尝试以下方式:

 - (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方法来源:调整 UIImage 大小的最简单方法?

于 2016-03-02T23:26:15.013 回答
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];
于 2015-10-06T16:57:35.980 回答
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!

    }

`

于 2017-09-05T07:36:58.220 回答