美文网首页
swift sdwebiamge圆角裁切封装 exension

swift sdwebiamge圆角裁切封装 exension

作者: 叫我周杰伦 | 来源:发表于2021-02-17 10:52 被阅读0次
    
    // MARK:sdwebimage圆角裁切封装
    
    extension UIImageView{
    
        funcsd_setImageWithURL(urlStr:NSString,cornerRadius:CGFloat){
    
            if(urlStr.length<=0) {return}
    
    
    
            leturl =NSURL.init(string: urlStrasString)
    
                    ifcornerRadius!=0.0{
    
                        // 有圆角,读取圆角的缓存图片
    
                        let  cacheurlStr = urlStr.appending("radius=%.1f\(cornerRadius)")
    
    
    
                        letcacheImage =SDImageCache.shared().imageFromDiskCache(forKey: cacheurlStr)
    
                        ifcacheImage  !=nil{
    
                            self.image= cacheImage;
    
                        }else{
    
                            SDWebImageManager.shared().loadImage(with: urlasURL?, options:SDWebImageOptions.retryFailed) { (receivedSize, expectedSize, targetURL)in
    
    
    
                            } completed: { (image, data, error, cacheType, finished, imageURL)in
    
                                iffinished==true{
    
                                    image?.imageWithCornerRadius(cornerRadius: cornerRadius, callBack: { (radiusImage)in
    
                                        self.image= radiusImage;
    
                                        SDImageCache.shared().store(radiusImage, forKey: cacheurlStr, completion:nil)
    
                                    })
    
                                }
    
                            }
    
                        }
    
                    }
    
                    else{
    
    
    
                        self.sd_setImage(with: urlasURL?, placeholderImage:nil, options:SDWebImageOptions.retryFailed) { (image, error, cacheType, imageURL)in
    
                        }
    
                    }
    
        }
    
    }
    
    // MARK: 圆角裁切
    
    extension UIImage{
    
        // MARK:裁切为圆形
    
        ///裁切为圆形 图片本身应为正方形
    
        funccirclemage(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) ->())    {
    
            ifcornerRadius<=0{
    
                return
    
            }
    
            DispatchQueue.global().async{
    
                // 开始裁切圆角
    
                letbounds =CGRect(x:0, y:0, width:self.size.width, height:self.size.height);
    
                UIGraphicsBeginImageContextWithOptions(self.size, false, UIScreen.main.scale);
    
                   letcontext =UIGraphicsGetCurrentContext();
    
                letpath =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{
    
            ifcornerRadius<=0{returnself}
    
    
    
            letoriginImage =self;
    
            // 开始裁切圆角
    
            letbounds =CGRect(x:0, y:0, width:self.size.width, height:self.size.height);
    
            UIGraphicsBeginImageContextWithOptions(self.size, false, UIScreen.main.scale);
    
               let context = UIGraphicsGetCurrentContext();
    
            letpath =UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius)
    
    
    
            context?.addPath(path.cgPath)
    
            context?.clip()
    
            originImage.draw(in: bounds)
    
               let image = UIGraphicsGetImageFromCurrentImageContext()
    
               UIGraphicsEndImageContext()
    
            returnimage!
    
        }
    
    
    
        func originalRenderImage() -> UIImage {
    
            self.withRenderingMode(.alwaysOriginal)
    
            return self
    
        }
    
    
    
    }
    
    

    相关文章

      网友评论

          本文标题:swift sdwebiamge圆角裁切封装 exension

          本文链接:https://www.haomeiwen.com/subject/utsixltx.html