在我的viewcontroller中我有:
@IBOutlet var worldmapview: Worldmapview!
var eventLayer:CALayer=CALayer();
和一些功能:
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以相同的方式更新了代码,它立即显示。我不明白其中的区别。在后台任务中创建时,不显示图片。
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.
但是激活最后一行,图片不会出现。可能它与它有关?看起来calayer的内容已经消失了?
答案 0 :(得分:1)
据我了解,您在后台创建CALayer
,和也将该图层添加到后台的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()
})
})