【问题标题】:How to use SDWebImage to draw image in cgcontext如何使用 SDWebImage 在 cgcontext 中绘制图像
【发布时间】:2015-08-15 11:22:39
【问题描述】:

我想使用 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 中

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

【问题讨论】:

    标签: ios swift cgcontext sdwebimage


    【解决方案1】:

    如果我猜对了,你想在调用 drawRect 时绘制 SDWebImage

    现在的问题是……绘图必须是同步的。您不能随时绘制屏幕:D 这是您正在尝试但没有看到的内容。

    (您需要更改整个“想法”,以便在被要求时实际绘制(当调用 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)
                }
              }
           }
       }
    

    【讨论】:

    • 下载后 sd_setImageWithURL 中的块也没有运行@Daij-Djan
    • 它是异步运行的
    • 我知道,我已经等了三分钟,但是当我再次运行它时,出现图像
    • 这是问题..是否已加载不是问题..尽管您还应该保留用于加载图像的 imageView,因为 arc 会释放它,这可能停止所有加载
    • 那么不明白 - 抱歉
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多