0

Hello I am using SDWebImage in my app. This is my code to make the image in circle

extension UIImage {
var circle: UIImage? {
        let square = CGSize(width: min(size.width, size.height), height: min(size.width, size.height))
        let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: square))
        imageView.contentMode = .ScaleAspectFill
        imageView.image = self
        imageView.layer.cornerRadius = square.width/2
        imageView.layer.masksToBounds = true
        UIGraphicsBeginImageContext(imageView.bounds.size)
        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        imageView.layer.renderInContext(context)
        let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return result
    }
}

copied it from here

I used to do images in cicle like this

let profilePicture = UIImage(data: NSData(contentsOfURL: NSURL(string:"https://i.stack.imgur.com/Xs4RX.jpg")!)!)!

profilePicture.circle

But Now As I am using SDWebImage its not working

 cell.profileImageView.sd_setImageWithURL(UIImage().absoluteURL(profileImageUrl), placeholderImage: UIImage.init(named: "default-profile-icon")?.circle!)

Please let me know how can I make this extension work for SDWebImage

Community
  • 1
  • 1
hellosheikh
  • 2,929
  • 8
  • 49
  • 115

1 Answers1

3

You can use the SDWebImageManager to download the image or take it from the cache and apply the circle in the completion block like this:

SDWebImageManager.sharedManager().downloadWithURL(NSURL(string:"img"), options: [], progress: nil) { (image:UIImage!, error:NSError!, cacheType:SDImageCacheType, finished:Bool) -> Void in
    if (image != nil){
        let circleImage = image.circle
        cell.profileImageView.image = circleImage
    }
}

Or you can use the version of the sd_setImageWithURL method that takes a completion block as a parameter

let completionBlock: SDWebImageCompletionBlock! = {(image: UIImage!, error: NSError!, cacheType: SDImageCacheType!, imageURL: NSURL!) -> Void in
    if (image != nil){
        let circleImage = image.circle
        cell.profileImageView.image = circleImage
    }
}
cell.profileImageView.sd_setImageWithURL(UIImage().absoluteURL(profileImageUrl), placeholderImage: UIImage.init(named: "default-profile-icon")?.circle!, completed: completionBlock)
LorenzOliveto
  • 7,796
  • 1
  • 20
  • 47
  • Dude YOU ARE AWESOME BIGTIME. to be honest after not getting a answer after an hour I thought I never be able to use this extenion. YOU ARE GREAT. THANKS THANKS THANK BIG TIME – hellosheikh Feb 26 '16 at 18:39