0

I am converting custom view load from xib into UIImage to show as a marker icon.it is done but the problem is to when doing this in for loop to show 100 markers UI get stuck is it because of loading xib custom view?

below is the function calling in for loop

func generateSpot(Prop: Property) -> Spot {

    let latStr = Prop.propLat
    let langStr = Prop.propLang as NSString
    let langDbl : Double = Double(langStr.floatValue)
    let latDbl : Double  = Double(latStr.floatValue)

    let marker:GMSMarker = GMSMarker()
    marker.position = CLLocationCoordinate2DMake(langDbl,latDbl)
    marker.appearAnimation = kGMSMarkerAnimationPop
    marker.title = Prop.propBuildingName as String
    marker.snippet = Prop.propCode as String
    marker.infoWindowAnchor = CGPointMake(0.44, 0.45)

    let sizeVar:CGSize = CGSize(width: 41, height: 37)

    let customeMarker :CustomMarker = NSBundle.mainBundle().loadNibNamed("CustomView", owner: self.view, options: nil).first! as! CustomMarker

    customeMarker.frame = CGRectMake(customeMarker.frame.minX,customeMarker.frame.minY,customeMarker.frame.width,customeMarker.frame.height)
    customeMarker.mappedNumbers.layer.masksToBounds = true
    customeMarker.mappedNumbers.layer.cornerRadius = 8
    customeMarker.mappedNumbers.text = Prop.propSequence as String

    if Prop.propStatus == self.constants!.status.STATUS_COMPLETED_XML
    {
        customeMarker.mappedNumbers.backgroundColor = self.utility!.uicolorFromHex(0x70b420)
        customeMarker.arrowImage.tintColor = self.utility!.uicolorFromHex(0x70b420)
    }

    if Prop.propStatus == self.constants!.status.STATUS_DRAFT_XML
    {
        customeMarker.mappedNumbers.backgroundColor = self.utility!.uicolorFromHex(0xd2cb46)
        customeMarker.arrowImage.tintColor = self.utility!.uicolorFromHex(0x70b420)


    }
    if Prop.propStatus == self.constants!.status.STATUS_PENDING_XML
    {
        customeMarker.mappedNumbers.backgroundColor = self.utility!.uicolorFromHex(0xd44646)
        customeMarker.arrowImage.tintColor = self.utility!.uicolorFromHex(0xd44646)


    }


    UIGraphicsBeginImageContextWithOptions(sizeVar, false, 0)
    customeMarker.drawViewHierarchyInRect(customeMarker.bounds, afterScreenUpdates: true)

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    marker.icon = newImage
    marker.map = self.googleMapView
    let spotObje:Spot = Spot()
    spotObje.location = marker.position
    spotObje.marker = marker
    return spotObje
}
Bhumika
  • 876
  • 7
  • 20
Nitisha Sharma
  • 239
  • 4
  • 14
  • do it in a background thread – Shubhank Jun 24 '16 at 12:05
  • tried in background thread but error comes This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release. – Nitisha Sharma Jun 24 '16 at 13:07
  • please show updated code in a separate http://gist.github.com link – Shubhank Jun 24 '16 at 13:09
  • https://gist.github.com/nitisha1/0763585550ed6be1cfa931408232f180 – Nitisha Sharma Jun 24 '16 at 13:36
  • is this code loading things on bg thread ? to me looks like everything being done on main thread ? – Shubhank Jun 24 '16 at 13:40
  • sorry ,updated check again – Nitisha Sharma Jun 24 '16 at 13:53
  • okay i checked. can you put breakpoints and see on which line it shows the autolayout error. there are several things going on it like marker frame manipulation, layer etc so not sure which UI part is throwing error – Shubhank Jun 24 '16 at 14:12
  • loading xib in background thread only no marker do but still unable to interect with UI as freely like i possible in android or is there any concept of handlers passing message after process completion like android in swift? – Nitisha Sharma Jun 27 '16 at 10:13

1 Answers1

0

solved replaced

 customeMarker.drawViewHierarchyInRect(customeMarker.bounds, afterScreenUpdates: true)

with

customeMarker.layer.renderInContext(UIGraphicsGetCurrentContext()!)

Reference : drawViewHierarchyInRect:afterScreenUpdates: delays other animations

Community
  • 1
  • 1
Nitisha Sharma
  • 239
  • 4
  • 14