所以我真的很困惑。我用过:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString* AnnotationIdentifier = @"Annotation";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (!pinView) {
MKPinAnnotationView *customPinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
if (annotation == mapView.userLocation){
customPinView.image = [UIImage imageNamed:@"303761_4417778996801_94858213_n.jpg"];
[customPinView.layer setMasksToBounds:NO];
[self setRoundedView:customPinView toDiameter:kPictureDiameter];
[customPinView.layer setBorderColor:[UIColor whiteColor].CGColor];
[customPinView.layer setBorderWidth:5.0f];
[customPinView.layer setShadowColor:[UIColor blackColor].CGColor];
[customPinView.layer setShadowOpacity:1.0f];
[customPinView.layer setShadowRadius:20.0f];
[customPinView.layer setShadowOffset:CGSizeMake(0, 0)];
[customPinView setBackgroundColor:[UIColor whiteColor]];
}
return customPinView;
} else {
pinView.annotation = annotation;
}
return pinView;
}
将我自己的图像添加到用户位置图钉。那行得通。但有两个问题:
阴影不起作用。
我为用户位置图钉设置的图像偏离中心,如下所示: user location picture
我尝试过使用 CGRect、CGPoint、设置框架、中心等(尽管我还没有尝试更改边界),但没有任何变化。
任何帮助将不胜感激。
最佳答案
关于图像偏离中心,有两个想法:
当使用我自己的图像进行注释时,我通常使用
MKAnnotationView
,而不是MKPinAnnotationView
。当我尝试使用MKPinAnnotationView
时,我的图像不再居中。如果最坏的情况发生,您可以使用
centerOffset
属性。如果您使用MKAnnotationView
,我认为您不需要这样做。
关于你的影子:
20 的阴影半径太大了,以至于它非常分散,以至于您几乎看不到它。当我使用 20 时,我几乎看不出来。在 10 时,它开始变得更加明显,在 5 时更明显。我认为这不是真正的问题,但当你开始尝试时,请使用一个数字这里足够低,所以你可以清楚地看到阴影。
话虽如此,我认为这不是问题所在。我对图像舍入算法表示怀疑。你能尝试暂时注释掉它,看看你的影子是否出现吗?但我在
MKAnnotationView
图像上使用阴影,效果很好。您能与我们分享您的舍入算法吗?如果这不起作用,该图像是否与叠加层重合? (即图片后面的圆圈是什么)。我会再次尝试暂时删除它,看看是否会改变行为。当我使用
MKCircleView
时,它工作正常,但我们要确保我们没有在那里做任何奇怪的事情(或使用任何其他绘图例程)。
关于ios - 自定义用户位置图像未以图钉为中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14640000/