【问题标题】:Objective-C: Pass a map snapshot to view controller in prepareForSegueObjective-C:将地图快照传递给 prepareForSegue 中的视图控制器
【发布时间】:2018-01-11 03:05:58
【问题描述】:

我正在尝试从 mapView 转到包含该 mapView 上所选位置的快照的页面。

这是我的prepareForSegue 方法:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"LocationConfirmSegue"]) {
        LocationConfirmationViewController *vc = segue.destinationViewController;

        MKCoordinateSpan span;
        span.longitudeDelta = mapFocusSpan;
        span.latitudeDelta = mapFocusSpan;

        MKCoordinateRegion region;
        region.center = *currentPinLocation;
        region.span = span;

        MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
        options.region = region;
        options.size = CGSizeMake(270, 180);

        MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
        [snapshotter startWithCompletionHandler:    ^(MKMapSnapshot * _Nullable snapshot, NSError * _Nullable error) {
            if (error) {
                NSLog(@"%@", error);
                return;
            }

            vc.mapSnapshot = snapshot.image;
        }];
    }
}

我可以在快照程序的完成处理程序之外将信息传递给目标 VC。代码也都可以正常工作——snapshot.image 确实返回 UIImage*——但是它没有被传递给目标视图控制器。当我尝试访问该视图控制器中的 mapSnapshot 属性时,我得到 (null)。

【问题讨论】:

  • 我能想到的一种可能的解决方案是将“选项”传递给目标视图控制器并在那里生成快照。不过,这感觉像是一种 hack,并且非常感谢另一种选择(我不必在目标视图控制器中导入 MapKit)。
  • 我不会在prepare(for:) 中生成快照,而是在您当前启动segue 的位置启动快照,然后在生成快照后,从snapshotter 的完成块触发segue

标签: ios objective-c mapkit mkmapsnapshotter


【解决方案1】:

利用苹果的OpenGLES截图,防止无法获取地图的图层图像或黑屏

-(UIImage *)zjcCutScreenImage {
NSInteger myDataLength = self.width * self.height * 4; 
GLubyte *buffer = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, self.width, self.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
for(int y = 0; y <self.height; y++) {
    for(int x = 0; x <self.width * 4; x++){
        buffer2[(JSNDHC - y) * self.width * 4 + x] = buffer[y * 4 * 1024 + x];
    }
}
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * self.height;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef = CGImageCreate(self.width, self.height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
UIImage *myImage = [UIImage imageWithCGImage:imageRef];
return myImage;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 2017-08-08
    • 1970-01-01
    相关资源
    最近更新 更多