如何使用SDWebImage在cgcontext中绘制图像

How to use SDWebImage to draw image in cgcontext

我想用 SDWebImage 下载一张图片 url 并在 UIView 中绘制(使用 cgcontext),但是下载完成后,没有任何反应

 func drawImage(){
    var imageView = UIImageView()
    println("drawimageaaaaa")
    imageView.sd_setImageWithURL(NSURL(string: drawObj.ctn!)!, placeholderImage: UIImage(named: "defaultphoto")!, completed: { (image:UIImage!, error:NSError!, type:SDImageCacheType, url:NSURL!) -> Void in
        println("drawimagebbbb")
        var context = UIGraphicsGetCurrentContext();
        CGContextSaveGState(context)
        var cgimg = image!.CGImage;
        var rect = CGRect(x: drawObj.points![0].x,y: drawObj.points![0].y,width: drawObj.currentWidth!,height: drawObj.currentHeight!)
        CGContextTranslateCTM(context, rect.origin.x, rect.origin.y)
        CGContextScaleCTM(context, 1.0, -1.0)
        CGContextRotateCTM(context,-CGFloat(Double(drawObj.angle!)*M_PI/180))
        if drawObj.isFlip == true{
            CGContextTranslateCTM(context, -rect.width/2, rect.height/2)
        }else{
            CGContextTranslateCTM(context, -rect.width/2, -rect.height/2)
        }
        CGContextTranslateCTM(context, -rect.origin.x, -rect.origin.y)
        CGContextDrawImage(context, rect, cgimg);
        CGContextRestoreGState(context)
    })
  }


  override func drawRect(rect: CGRect) {
       for drawObj in drawObjs {
          if drawObj.drawPara!.drawType == MouseStatus.attach{
             drawImage(drawObj)
          }
       }
   }

现在我再次尝试 运行 drawImage 函数(相同 url)图像出现在 UIView

drownload完成后如何在UIView中绘制?

如果我没看错你想在调用 drawRect 时绘制 SDWebImage

现在的问题是......绘图必须是同步的。您不能随心所欲地绘制屏幕 :D 这是您正在尝试但看不到的内容。

(您需要更改整个 'idea' 以便在被要求时(调用 drawRect 时)实际绘制

例如

func drawImage(drawObj) {
        println("drawimagebbbb")
        var context = UIGraphicsGetCurrentContext();
        CGContextSaveGState(context)
        var cgimg = image!.CGImage;
        var rect = CGRect(x: drawObj.points![0].x,y: drawObj.points![0].y,width: drawObj.currentWidth!,height: drawObj.currentHeight!)
        CGContextTranslateCTM(context, rect.origin.x, rect.origin.y)
        CGContextScaleCTM(context, 1.0, -1.0)
        CGContextRotateCTM(context,-CGFloat(Double(drawObj.angle!)*M_PI/180))
        if drawObj.isFlip == true{
            CGContextTranslateCTM(context, -rect.width/2, rect.height/2)
        }else{
            CGContextTranslateCTM(context, -rect.width/2, -rect.height/2)
        }
        CGContextTranslateCTM(context, -rect.origin.x, -rect.origin.y)
        CGContextDrawImage(context, rect, cgimg);
        CGContextRestoreGState(context) 
}

func loadImage(drawObj){
    var imageView = UIImageView()
    println("drawimageaaaaa")
    imageView.sd_setImageWithURL(NSURL(string: drawObj.ctn!)!, placeholderImage: UIImage(named: "defaultphoto")!, completed: { (image:UIImage!, error:NSError!, type:SDImageCacheType, url:NSURL!) -> Void in
        dispatch_async(dispatch_get_main_queue() ) {
            drawObj.image = image
            self.setNeedsDisplay() //tell iOS to ready for drawing
        }
    })

drawObj.imageViewUsedForLoading = imageView //保留! }

  override func drawRect(rect: CGRect) {
       for drawObj in drawObjs {
          if drawObj.drawPara!.drawType == MouseStatus.attach{
            if drawObj.image /*you have to manage this somehow*/ {
                drawImage(drawObj)
            }
            else { 
                loadImage(drawObj)
            }
          }
       }
   }