resize sdwebImage swift
Asked Answered
O

4

5

Hello I am displaying images on my app using sdwebImage. I have a code here to resize the image

func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage {

    let scale = newWidth / image.size.width
    let newHeight = image.size.height * scale
    UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight))
    image.drawInRect(CGRectMake(0, 0, newWidth, newHeight))
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage
}

The problem is the above function accepts the UIImage as parameter and sdwebimage accepts the URL. How can I call the above resize function in sdwebimage. or in short how Can I resize the image that are presenting through sdwebImage here

   cell.profileImageView.sd_setImageWithURL(UIImage().absoluteURL(profileImageUrl as! String), placeholderImage: UIImage.init(named: "default-profile-icon")?.circle!, completed: completionBlock)
Ozieozkum answered 28/3, 2016 at 10:23 Comment(5)
sdwebimage accept the URL but you applied in that imageview in my knowledge is there no problemReitz
sorry what does it mean. I am sorry I didn't get youOzieozkum
in my knowledge in your code is correct , one doubt where you called resizeImage methodReitz
yes That is what I am asking. where to call the resize image ?Ozieozkum
The library doesn't support a good way to resize the images. Callback is performed on the main thread and you don't want to resize the images there, the implemented caching will also mean that the resize operation will be needed each time the load is requested. You'll have to enhance the library to support resizing, one of the better options IMO is to implement your own resizing category similar to the loading category provided by the library.Kistner
R
8

do like

cell.profileImageView.sd_setImageWithURL(
    NSURL(string: profileImageUrl as! String),
    placeholderImage: UIImage.init(named: "default-profile-icon"),
    options: nil,
    progress: nil,
    completed: { (image: UIImage?, error: NSError?, cacheType: SDImageCacheType!, imageURL: NSURL?) in

        guard let image = image else { return }
        print("Image arrived!")
        cell.profileImageView.image = resizeImage(image, newWidth: 200)
    }
)
Reitz answered 28/3, 2016 at 10:46 Comment(3)
inside the block call that resize methodReitz
your problem is resolved or not, if my answer is not useful reply once i delete my answerReitz
With your solution resized image will be set to the imageview. but in chache there is actual sized image so there is still chance to memory issue. Solution would be image downloader delegate and return resized image like @Evgeny Karev solutionSquaw
P
1

SDWebImage supports image handling directly through its SDWebImageManagerDelegate protocol. You can use imageManager:transformDownloadedImage:withURL: method to transform the downloaded image.

You can set the image manager delegate like this:

SDWebImageManager.sharedManager.delegate = self;
Peasecod answered 28/3, 2016 at 10:50 Comment(1)
could you please write complete code or edit my code.Ozieozkum
C
1

Use shared SDWebImageManager is not good idea. Some processes can download images in this time.

My swift 3 example with custom SDWebImageManager in the class and custom SDWebImageManagerDelegate resizer.

import SDWebImage

class ImageResizer: NSObject, SDWebImageManagerDelegate {
    private func resizeImage(_ image: UIImage, newHeight: CGFloat) -> UIImage {
        let scale = newHeight / image.size.height
        let newWidth = image.size.width * scale

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

        return newImage!
    }

    public func imageManager(_ imageManager: SDWebImageManager, transformDownloadedImage image: UIImage?, with imageURL: URL?) -> UIImage? {
        guard let _image = image else {
            return nil
        }

        return resizeImage(_image, newHeight: 20)
    }
}


class BasicTrainView: XibView {
    static let imageManager: SDWebImageManager = SDWebImageManager()
    static let imageResizer = ImageResizer()

    func xxx() {
        BasicTrainView.imageManager.delegate = BasicTrainView.imageResizer
        BasicTrainView.imageManager.loadImage(with: logoURL, options: [], progress: nil) { (image, _, error, sdImageCacheType, _, url) -> Void in
            guard let _image = image else {
                self.carrierLogoImageView.image = nil
                return
            }

            self.carrierLogoImageView.image = _image
        }
    }
}
Crackerjack answered 1/8, 2017 at 22:9 Comment(0)
P
0

SDWebImage has this functionality built in. This is how to use it

let imageSize = cell.fanartImageView.bounds.size * UIScreen.main.scale

let transformer = SDImageResizingTransformer(size: imageSize, scaleMode: .fill)

cell.fanartImageView.sd_setImage(with: url, placeholderImage: image,
                                options: SDWebImageOptions(rawValue: 0),
                                context: [.imageTransformer: transformer],
                                progress: nil) { (image, error, cache, url) in
    if error != nil {
        // handle error
    }
}
Polled answered 30/1, 2021 at 12:53 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.