1

我试图从一个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)。

4

1 回答 1

0

利用苹果的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;
}
于 2018-01-11T05:30:08.143 回答