2015-10-21 12 views
7

Ich habe das persönliche Bild anstelle der traditionellen roten Pin. Wenn ich die Karte öffne, um den Pin anzuzeigen, bedeckt das Bild die gesamte Map. Gibt es eine maximale Größe für das Pin-Image oder wie integriere ich etwas in den Code, um ihn an den klassischen Standard-Pin anzupassen?Größe Bild Pin Annotation

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 
} 

Antwort

19

Es gibt keine maximale Größe des Pinbildes. Sie müssen die Größe von UIImage ändern.

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

es sagt ** CGRectMake ** nicht in SWFT ist – Saneth

+0

pinImage.draw (in: CGRect (x: 0, y: 0, Breite: Size.width, Höhe: Size.height)) –

+0

würde ich Verwenden Sie: anView? .frame.size = CGSize (Breite: 30, Höhe: 40) Anstelle von allem drawContext stuff –

Verwandte Themen