gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:08:40 25 4
gpt4 key购买 nike

我想使用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中绘制它?

最佳答案

如果我猜对了,你想在调用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)
}
}
}
}

关于ios - 如何使用SDWebImage在cgcontext中绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32024214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com