gpt4 book ai didi

ios - MKMapSnapshotter 如何设置pin Image

转载 作者:行者123 更新时间:2023-11-29 12:09:56 27 4
gpt4 key购买 nike

我创建图钉图像的代码如下:

 MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
options.region = MKCoordinateRegionMakeWithDistance(userLocation, 300, 300) ;
options.size = CGSizeMake(320,180);
options.scale = [UIScreen mainScreen].scale;

MKMapSnapshotter *snapShotter = [[MKMapSnapshotter alloc] initWithOptions:options];

[snapShotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
//
if (error) {
NSLog(@"err = %@",[error description]);
}

UIImage *img = snapshot.image;

MKAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:nil];
CGPoint coordinatePoint = [snapshot pointForCoordinate:userLocation];


coordinatePoint.x += pin.centerOffset.x - (CGRectGetWidth(pin.bounds) / 2.0);
coordinatePoint.y += pin.centerOffset.y - (CGRectGetHeight(pin.bounds) / 2.0);

UIGraphicsBeginImageContextWithOptions(img.size, YES, img.scale);
{
[img drawAtPoint:CGPointZero];
[pin.image drawAtPoint:coordinatePoint];
_imgViewMapThubbnail.image = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();
_imgViewMapThubbnail.layer.cornerRadius = 10 ;
_imgViewMapThubbnail.layer.masksToBounds = true ;

}];

此代码是正常的红色引脚,位置几乎正确。我想要我的自定义 pin,所以我添加了如下代码

pin.image = [UIImage imageNamed:@"MY_IMAGE_NAME"];

Pin 图像已更改,并且可以正常工作。但是,我的位置有些不同。我不知道,如何调整我的代码或固定图像大小以将其设置到正确的位置?

最佳答案

看到这是一个老问题,对于任何来这里自定义图钉图像的人来说,这里是上述示例的修订代码,可以将图钉呈现在正确的位置。

CGPoint coordinatePoint = [snapshot pointForCoordinate:newCenter];
UIImage *pin = [UIImage imageNamed:@"pin"];
CGPoint centerOffset = CGPointMake(0, -pin.size.height / 2);
coordinatePoint.x += centerOffset.x - (pin.size.width / 2.0);
coordinatePoint.y += centerOffset.y - (pin.size.height / 2.0);

UIGraphicsBeginImageContextWithOptions(img.size, YES, img.scale);
{
[img drawAtPoint:CGPointZero];
[pin drawAtPoint:coordinatePoint];
_imgViewMapThubbnail.image = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();

关于ios - MKMapSnapshotter 如何设置pin Image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33755097/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com