2015-10-21 16 views
7

Ho inserito l'immagine personale al posto del tradizionale pin rosso. Quando apro la mappa per visualizzare il pin, l'immagine copre l'intera mappa. C'è una dimensione massima dell'immagine del pin o come faccio ad integrare qualcosa nel codice per adattarlo al pin classico standard?Dimensioni annotazione pin immagine

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    if annotation is MKUserLocation { 
     return nil 
    } 

    let annotationIdentifier = "SomeCustomIdentifier" // use something unique that functionally identifies the type of pin 

    var annotationView: MKAnnotationView! = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) 

    if annotationView != nil { 
     annotationView.annotation = annotation 
    } else { 
     annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) 

     annotationView.image = UIImage(named: "pin maps.png") 

     annotationView.canShowCallout = true 
     annotationView.calloutOffset = CGPointMake(-8, 0) 

     annotationView.autoresizesSubviews = true 
     annotationView.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure) as UIView 
    } 

    return annotationView 
} 

risposta

19

Non c'è una dimensione massima dell'immagine pin. Devi ridimensionare UIImage.

let annotationIdentifier = "SomeCustomIdentifier" 
    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) 
    if annotationView == nil { 
     annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) 
     annotationView?.canShowCallout = true 

     // Resize image 
     let pinImage = UIImage(named: "pin maps.png") 
     let size = CGSize(width: 50, height: 50) 
     UIGraphicsBeginImageContext(size) 
     pinImage!.drawInRect(CGRectMake(0, 0, size.width, size.height)) 
     let resizedImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 

     annotationView?.image = resizedImage 

     let rightButton: AnyObject! = UIButton(type: UIButtonType.DetailDisclosure) 
     annotationView?.rightCalloutAccessoryView = rightButton as? UIView 
    } 
    else { 
     annotationView?.annotation = annotation 
    } 
+0

si dice ** ** CGRectMake non sia in swft – Saneth

+0

pinImage.draw (in: CGRect (x: 0, y: 0, larghezza: size.width, altezza: size.height)) –

+0

avrei utilizzare: anView? .frame.size = CGSize (larghezza: 30, altezza: 40) Invece di tutte le cose drawContext –

Problemi correlati