Reputation: 31
I am displaying image using SDWebImage. You can check below code.
cell.imgPackage.sd_setImage(with: URL(string: "MY URL STRING")! as URL, placeholderImage: UIImage(named: "testPlaceholder.png"), options: .refreshCached)
I need to resize width and height of server image and set in tableview cell image.
My server image resolution is around width - 1519 height - 950
and tableview having - width - 320 height - 150
let me know if you need any clarification.
Upvotes: 2
Views: 4101
Reputation: 1251
A little too late, but for others, if you already know the dimensions you want, you can use the
SDImageResizingTransformer
let transformer = SDImageResizingTransformer(size: CGSize(width: 300, height: 300), scaleMode: .aspectFill)
yourImage.sd_setImage(with: URL(string: "url_to_your_image"), placeholderImage: nil, context: [.imageTransformer: transformer])
Upvotes: 1
Reputation: 267
Try this code to scale your image.
import UIKit
import SDWebImage
class ViewController: UIViewController {
@IBOutlet weak var img: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
img.sd_setImage(with: URL(string: "https://images.techhive.com/images/article/2016/06/macos_sierra_big-sierra-100667928-orig.png"), placeholderImage: nil) { (image: UIImage?, error: Error?, cacheType:SDImageCacheType!, imageURL: URL?) in
//new size
self.img.image = self.resizeImage(image: image!, newWidth: 600)
//original size
//self.img.image = image
}
}
func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage {
let scale = newWidth / image.size.width
let newHeight = image.size.height * scale
UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))
image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
}
Upvotes: 3