Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to resize UIImage with SDWebImage?

I want to resize image with SDWebImage.

I tryed to use imageManager delegate method of SDWebImageManagerDelegate.

But, I can't figure out how to call imageManager method.

my code is below.

import UIKit
import SDWebImage

class testGraphic2ViewController: UIViewController, SDWebImageManagerDelegate {
    @IBOutlet weak var imageView: UIImageView!

    let postImageUrlStr = "https://firebasestorage.googleapis.com/v0/b/instagramclone-be6eb.appspot.com/o/posts%2FFBAE2BE9-F429-4D39-978C-DD2E24BF062D?alt=media&token=7b41569b-5451-4728-942c-6d43ec50350c"

    override func viewDidLoad() {
        super.viewDidLoad()
        SDWebImageManager.shared().delegate = self
        SDWebImageManager.shared().loadImage(with: URL(string: postImageUrlStr), options: [], progress: nil) { (image, data, error, cacheType, finished, url) in
            self.imageView.image = image
        }
    }

    func imageManager(_ imageManager: SDWebImageManager, transformDownloadedImage image: UIImage?, with imageURL: URL?) -> UIImage? {
        print("imageManager is called!")
        guard let image = image, let imageURL = imageURL else {
            return nil
        }
        if (imageURL.lastPathComponent == "large" && (image.size.width > 1000 || image.size.width > 1000)) {
            let targetSize = CGSize(width: 1000, height: 1000)
            UIGraphicsBeginImageContextWithOptions(targetSize, false, image.scale)
            image.draw(in: CGRect(origin: .zero, size: targetSize))

            let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return scaledImage
        } else {
            return image
        }
    }

how can I call imageManager method?

like image 254
kou Avatar asked Dec 01 '22 10:12

kou


2 Answers

It's pretty simple in SDWebImage 5.x, you can use in build SDImageResizingTransformer

let transformer = SDImageResizingTransformer(size: CGSize(300, 300), scaleMode: .fill)
let imageView: UIImageView
imageView.sd_setImage(withURL: url, placeholderImage: nil, context: [.imageTransformer: transformer])
like image 191
Satish Avatar answered Dec 05 '22 10:12

Satish


As SDWebImage will return a UIImage after download, hence we can create an extension of UIImage and use that extension to resize the image.

Swift3

extension UIImage{

func resizeImageWith(newSize: CGSize) -> UIImage {

    let horizontalRatio = newSize.width / size.width
    let verticalRatio = newSize.height / size.height

    let ratio = max(horizontalRatio, verticalRatio)
    let newSize = CGSize(width: size.width * ratio, height: size.height * ratio)
    UIGraphicsBeginImageContextWithOptions(newSize, true, 0)
    draw(in: CGRect(origin: CGPoint(x: 0, y: 0), size: newSize))
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage!
   }
}
like image 40
Samarth Kejriwal Avatar answered Dec 05 '22 08:12

Samarth Kejriwal