Pregunta

I have a problem with MKMapSnapshotter. I currently have two views, one with a UIButton, which shows a UIActivityViewController and one with a MKMapView, which shows current user location. The UIActivityViewController is used to share user's coordinates and a UIImage taken from MKMapView showing current location.

The picture is taken using MKMapSnapshotter with this code in the 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];
    }];
}

and saved to a file named: SOS_Map.png, as you can see. The problem is that if I try to load the .png file it doesn't come up in the UIActivityViewController and so in the Mail/Messages app. I'm sure that the picture has definitely been taken and that the code works [even because I have tested it with NSLogs], but I still can't load the image.

This is the code used to launch UIActivityViewController, adding a text and to load the UIImage in the 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];
}

Maybe it's a file directory problem? I don't know, can you please help me with this problem?

¿Fue útil?

Solución

Your call to -[NSData writeToFile:atomically:] is failing because you are not providing a writeable path. Try this:

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

Then, to load and share the image:

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];

Another, possibly simpler solution would be to keep the captured image in memory.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top