0

我有一个问题MKMapSnapshotter。我目前有两个视图,一个带有 a UIButton,它显示 a UIActivityViewController,一个带有 a MKMapView,它显示当前用户位置。UIActivityViewController用于共享用户的坐标,并从UIImage显示MKMapView当前位置获取。

图片是使用MKMapSnapshotter以下代码拍摄的secondViewController.m

- (void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered
{
    snapshotOptions = [[MKMapSnapshotOptions alloc] init];
    snapshotOptions.region = self.mapView.region;
    snapshotOptions.scale = [UIScreen mainScreen].scale;
    snapshotOptions.size = self.mapView.frame.size;

    snapShotter = [[MKMapSnapshotter alloc] initWithOptions:snapshotOptions];

    [snapShotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error)
    {
        snapshotImage = snapshot.image;
        data = UIImagePNGRepresentation(snapshotImage);
        [data writeToFile:@"SOS_Map.png" atomically:YES];
    }];
}

并保存到名为: 的文件SOS_Map.png中,如您所见。问题是,如果我尝试加载.png文件,它不会出现在UIActivityViewController邮件/消息应用程序中。我确信这张照片确实已经拍摄并且代码有效[即使因为我已经用 测试过它NSLogs],但我仍然无法加载图像。

这是用于启动UIActivityViewController、添加文本和加载的UIImage代码firstViewController.m

- (IBAction)shareCoordinates:(id)sender
{
    myBundle = [NSBundle mainBundle];
    pathToImage = [myBundle pathForResource:@"SOS_Map" ofType:@"png"];

    itemsToShare = [NSMutableArray new];
    [itemsToShare addObject:[NSString stringWithFormat:@"text]]; //Coordinates
    [itemsToShare addObject:[UIImage imageNamed:@"SOS_Map.png"]]; //Picture

    itemsToShareLoaded = [NSArray arrayWithArray:itemsToShare];

    _activityView = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare
                                                      applicationActivities:nil];

    [self presentViewController:_activityView animated:YES completion:nil];
}

可能是文件目录问题?我不知道,你能帮我解决这个问题吗?

4

1 回答 1

3

您的调用-[NSData writeToFile:atomically:]失败,因为您没有提供可写路径。尝试这个:

NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
NSURL *imageURL = [documentsURL URLByAppendingPathComponent:@"SOS_Map.png"];
[data writeToURL:imageURL atomically:YES];

然后,加载和共享图像:

NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
NSURL *imageURL = [documentsURL URLByAppendingPathComponent:@"SOS_Map.png"];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];

NSArray *itemsToShare = @[@"text", image];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];

另一种可能更简单的解决方案是将捕获的图像保存在内存中。

于 2014-01-20T18:08:05.563 回答