我对MKMapSnapshotter
有疑问。我目前有两个视图,一个显示UIButton
,一个显示UIActivityViewController
,另一个显示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];
}
的代码
{{1}}
也许这是文件目录问题?我不知道,你能帮我解决这个问题吗?
答案 0 :(得分: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];
另一种可能更简单的解决方案是将捕获的图像保存在内存中。