我对MKMapSnapshotter
有问题。我目前有两个视图,一个带有UIButton
的视图,该视图显示了UIActivityViewController
,另一种显示了MKMapView
,该视图显示了当前的用户位置。UIActivityViewController
用于共享用户的坐标,并从MKMapView
中获得的UIImage
显示当前位置。
使用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
,添加文本并在firstViewController.m
UIImage
的代码 - (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];
}
也许是文件目录问题?我不知道,你能帮我解决这个问题吗?
您对-[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];
另一个可能更简单的解决方案是将捕获的图像保存在内存中。