UIImage在后台任务中添加到CALayer时不显示

在我的视图控制器中我有:

@IBOutlet var worldmapview: Worldmapview! var eventLayer:CALayer=CALayer(); 

和一些function:

 func create_picture_layer(pathtopic:String) -> CALayer { let bm=getImage(pathtopic,fBitmapsizeSelected: 100)!; var calayer_for_picture=CALayer(); calayer_for_picture.frame=CGRectMake(100, 100, 100, 100); calayer_for_picture.contents = bm.CGImage; return calayer_for_picture; } func createCircleShape(x:CGFloat,y:CGFloat, size:CGFloat) -> CAShapeLayer { var test2:UIBezierPath=UIBezierPath(ovalInRect: CGRectMake(x - size/2, y - size/2, size, size)); var shapelayer:CAShapeLayer=CAShapeLayer(); shapelayer.backgroundColor=UIColor.clearColor().CGColor; //shapelayer.frame=CGRectMake(0, 0, 300, 300); shapelayer.fillColor=UIColor.darkGrayColor().CGColor; shapelayer.path=test2.CGPath; shapelayer.opaque=true return shapelayer; } func getImage(simagename: String, fBitmapsizeSelected:CGFloat) -> UIImage?{ var sPathToImage:String=AppProperties.sharedInstance.sPicturePath + simagename; let checkValidation = NSFileManager.defaultManager() if (!checkValidation.fileExistsAtPath(sPathToImage) || simagename.equals("")){ // Path to unknown image let sunknownimagename:String = "na/na.jpa"; sPathToImage=AppProperties.sharedInstance.sPicturePath + sunknownimagename; } let bm:UIImage=UIImage(contentsOfFile: sPathToImage)!; var bMapScaled:UIImage; if(!bm.isEqual(nil)){ let newSize:CGSize=CGSizeMake(fBitmapsizeSelected, fBitmapsizeSelected); bMapScaled = bm.imageByScalingToSize(newSize,contentMode: UIViewContentMode.ScaleAspectFill)!; return bMapScaled; }else{ return nil; } } 

在我的viewDidAppear(animated: Bool) method我尝试在worldmapview中显示图像

下面的代码工作正常,没有任何后台任务。 图像立即显示。

 // works let thepicturelayer=create_picture_layer("mypicpath") eventLayer.addSublayer(thepicturelayer); worldmapview.layer.addSublayer(eventLayer); eventLayer.setNeedsDisplay(); 

在后台任务和主要任务刷新它不适合我。 图片没有显示。 一段时间后,似乎,但并不总是。

 // doesn't work let qualityOfServiceClass = QOS_CLASS_BACKGROUND let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0) dispatch_async(backgroundQueue, { // geht auch mit Eventlayer let thepicturelayer=self.create_picture_layer() // This Shape doesn't appear. let circleshapelayer=self.createCircleShape(200, y: 200, size: 20) // This shape appears immediatelly. self.eventLayer.addSublayer(thepicturelayer); self.eventLayer.addSublayer(circleshapelayer); dispatch_async(dispatch_get_main_queue(),{ //assert(self.eventLayer.sublayers?.count > 0,"no layers in eventLayer !") // just added self.worldmapview.layer.addSublayer(self.eventLayer); //assert(self.worldmapview.layer.sublayers!.count > 0,"no layers in worldmapview!" ) // just added let sublayers=self.eventLayer.sublayers; if sublayers != nil { for l in self.eventLayer.sublayers! { l.setNeedsDisplay() } } //self.updateWorldMapGUI(); self.worldmapview.layer.addSublayer(self.eventLayer); //thepicturelayer.setNeedsDisplay(); //circleshapelayer.setNeedsDisplay(); self.eventLayer.setNeedsDisplay(); }) }) 

更新

我更新了一个新的CAShapelayer的代码添加相同的方式,这是立即显示。 我不明白区别。 在后台任务中创build图片时不显示。

UPDATE2

我意识到另外一些令我感到困惑的事情:下面的代码没有后台任务,如前所述:

  let thepicturelayer=create_picture_layer() eventLayer.addSublayer(thepicturelayer); worldmapview.layer.addSublayer(eventLayer); eventLayer.setNeedsDisplay(); //thepicturelayer.setNeedsDisplay(); // When adding this, the picture is not shown. 

但是激活最后一行,图片不会出现。 可能与它有关系? 看来这个玩家的内容不见了?

据我所知,你是在后台创buildCALayer并将该图层添加到后台的UI层次结构中。

认为所有的UI修改都要在main线程中发生是否合理? 如果是的话,试试这个:

 let qualityOfServiceClass = QOS_CLASS_BACKGROUND let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0) dispatch_async(backgroundQueue, { let thepicturelayer=self.create_picture_layer() dispatch_async(dispatch_get_main_queue(),{ self.eventLayer.addSublayer(thepicturelayer); assert(self.eventLayer.sublayers) // just added self.worldmapview.layer.addSublayer(self.eventLayer); assert(self.worldmapview.layer.sublayers) // just added self.worldmapview.setNeedsDisplay() }) })