其他分享
首页 > 其他分享> > SDWebImage裁切圆角

SDWebImage裁切圆角

作者:互联网

extension UIImageView{
    
    func sd_setImageWithURL(urlStr:String,cornerRadius:CGFloat){
        
        if(urlStr.count <= 0) {return}
        let url = URL(string: urlStr)
        if cornerRadius != 0.0{
            // 有圆角,读取圆角的缓存图片
            let  cacheurlStr = urlStr.appending("radius=%.1f\(cornerRadius)")
            let cacheImage = SDImageCache.shared.imageFromDiskCache(forKey: cacheurlStr)
            if cacheImage != nil{
                self.image = cacheImage;
            }else{
                SDWebImageManager.shared.loadImage(with: url, options:SDWebImageOptions.retryFailed) { (receivedSize, expectedSize, targetURL)in
                } completed: { (image, data, error, cacheType, finished, imageURL)in
                    if finished == true {
                        image?.imageWithCornerRadius(cornerRadius: cornerRadius, callBack: { (radiusImage)in
                            self.image = radiusImage;
                            SDImageCache.shared.store(radiusImage, forKey: cacheurlStr, completion:nil)
                        })
                    }
                }
            }
        }
        else{
            self.sd_setImage(with: url, placeholderImage:nil, options:SDWebImageOptions.retryFailed) { (image, error, cacheType, imageURL)in
            }
        }
    }
}

// MARK: 圆角裁切

extension UIImage{
    
    // MARK:裁切为圆形
    ///裁切为圆形 图片本身应为正方形
    func circlemage(callBack:@escaping(_ image:UIImage) ->()) {
        self.imageWithCornerRadius(cornerRadius:self.size.width*0.5) { (image)in
            callBack(image)
        }
    }
    // MARK:分线程裁切圆角
    ///分线程裁切圆角
    func  imageWithCornerRadius(cornerRadius:CGFloat,callBack:@escaping(_ image:UIImage) ->())    {
        if cornerRadius <= 0 {
            return
        }
        
        DispatchQueue.global().async{
            // 开始裁切圆角
            let bounds = CGRect(x:0, y:0, width:self.size.width, height:self.size.height);
            UIGraphicsBeginImageContextWithOptions(self.size, false, UIScreen.main.scale);
            let context = UIGraphicsGetCurrentContext();
            let path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius)
            context?.addPath(path.cgPath)
            context?.clip()
            self.draw(in: bounds)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            //回到主线程刷新UI
            DispatchQueue.main.async(execute: {
                callBack(image ?? UIImage())
            })
        }
    }
    // MARK:主线程裁切圆角
    ///主线程裁切圆角
    func  imageWithCornerRadius(cornerRadius:CGFloat) ->UIImage{
        if cornerRadius <= 0 { return self }
        let originImage = self;
        // 开始裁切圆角
        let bounds = CGRect(x:0, y:0, width:self.size.width, height:self.size.height);
        UIGraphicsBeginImageContextWithOptions(self.size, false, UIScreen.main.scale);
        let context = UIGraphicsGetCurrentContext();
        let path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius)
        context?.addPath(path.cgPath)
        context?.clip()
        originImage.draw(in: bounds)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
    func originalRenderImage() -> UIImage {
        self.withRenderingMode(.alwaysOriginal)
        return self
    }
}

如果使用的是Swift的话,还是使用Kingfisher,它自带给图片添加圆角的属性。

标签:圆角,裁切,self,cornerRadius,UIImage,image,SDWebImage
来源: https://www.cnblogs.com/chaostudy/p/16482014.html