ios - 在 MKMapView 上添加具有模糊效果的圆圈

标签 ios mkmapview core-graphics mapkit core-image

我正在尝试在 map 上的叠加层上添加模糊效果。我实际上需要在 map 上的一个圆圈上的模糊效果,我用来获得它的方法并不那么重要。

我有一个从 MKCircleRenderer 扩展的类,我想在它覆盖的 map 上添加模糊效果。

我正在尝试使用 -fillPath:inContext: 方法,但我对 Core Graphics 和 Core Image 的无知让我无所适从,我真的迷失了这个问题。

我尝试使用 CIFilter,为此我需要一个 CIImage,我试图从上下文中创建它。但是我找不到从上下文中创建 CGBitmapContextCGImage 或任何其他类的方法。我尝试过的任何方法都会导致 NULL,但没有关于原因的更多详细信息。我不记得我尝试过的所有内容,所以我很抱歉没有指出任何相关内容。

我的类(class)目前实现了一种并没有做太多事情的方法:

- (instancetype)initWithOverlay:(id<MKOverlay>)overlay {
    if (self = [super initWithOverlay:overlay]) {
        self.strokeColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1];
        self.fillColor = [UIColor colorWithRed:0.4 green:0.2 blue:0.2 alpha:0.1];
        self.lineWidth = 1;
    }
    return self;
}

另一种方法是使用自定义 MKAnnotation 并使用 UIVisualEffectView 在 View 上添加模糊效果。这种方法的难点在于在缩放时增加/减小尺寸。

这应该适用于 iOS 8+

编辑

在这种情况下,圆圈内侧后面的 map 应该是模糊的

最佳答案

所以我最终在叠加层上使用了 UIVisualEffectView。诀窍在于使用 CADisplayLink 来保持 View 就位。

这是一些完成这项工作的代码示例(它忽略了一些在应用程序上实际执行此操作时应该考虑的事情,例如删除链接,跟踪在 viewDidAppear 上完成的操作必须与可能在 viewWillDissapear 或其他东西上进行对称工作,我认为我可以使用 viewDidLoad,但在测试时是这样做的)。

#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <QuartzCore/QuartzCore.h>

@interface ViewController () {
    IBOutlet MKMapView *map;
    UIView *ov;
    MKCircle *c;
    UIVisualEffectView *ev;
}

@end

@implementation ViewController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    CLLocationCoordinate2D center = CLLocationCoordinate2DMake(40.7828647,-73.9675438);
    c = [MKCircle circleWithCenterCoordinate:center radius:1000];
    [map addOverlay:c];
    MKCoordinateSpan span = MKCoordinateSpanMake(0.07, 0.07);
    MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
    region = [map regionThatFits:region];
    [map setRegion:region animated:YES];

    ov = [[UIView alloc] init];
    ov.translatesAutoresizingMaskIntoConstraints = NO;
    ov.backgroundColor = [UIColor clearColor];
    ov.clipsToBounds = YES;
    ov.layer.borderWidth = 1;
    ov.layer.borderColor = [UIColor blackColor].CGColor;
    [map addSubview:ov];

    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    ev = [[UIVisualEffectView alloc] initWithEffect:blur];
    [ov addSubview:ev];

    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(update:)];
    [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)update:(CADisplayLink *)link {
    ov.frame = [map convertRegion:MKCoordinateRegionForMapRect(c.boundingMapRect) toRectToView:map];
    ov.layer.cornerRadius = ov.frame.size.height / 2;
    ev.frame = CGRectMake(0, 0, ov.frame.size.width, ov.frame.size.height);
}

@end

编辑:截图

关于ios - 在 MKMapView 上添加具有模糊效果的圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33264074/

相关文章:

ios - println(NSString(data : data, encoding : NSUTF8StringEncoding)) means in swift?)中的这些(响应、数据、错误)是什么

iOS:存储联系信息和 GPS 坐标的最佳方式

ios - UITableViewCell自定义CG画iOS 7

ios - 非圆形发光的 CGContextDrawRadialGradient

ios - 更改按钮状态时显示/隐藏 subview

iOS 如何添加配置文件

ios - 如何在MKMapView iOS上以编程方式显示用户位置?

ios - MKMapView 无限水平滚动

ios - 如何用手指移动线条?

ios - 无法从 PKPaymentAuthorizationViewController 委托(delegate)方法 didSelectShippingContact 内的 PKContact 访问电话号码、名字、街道