2014-01-20 34 views
0

我有一個問題MKMapSnapshotter。我目前有兩種觀點,一種是UIButton,顯示UIActivityViewController,另一種顯示MKMapView,顯示當前用戶的位置。 UIActivityViewController用於共享用戶的座標,UIImage取自MKMapView,顯示當前位置。MKMapSnapshotter的作品,但我不能共享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

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

UIImage也許這是一個文件目錄的問題?我不知道,你能幫我解決這個問題嗎?

回答

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

另一個,可能更簡單的解決辦法是讓所拍攝的圖像在內存中。

+0

你好@jonahb我試過你的代碼你發佈但結果它仍然是一樣的。該圖像被拍攝,但仍然沒有載入UIActivityViewController [和NSLogs確認這實際上它沒有被調用]。 這是一個有趣的想法,如果將圖像保存在內存中更容易,請告訴我如何做到這一點? – Aluminum

+1

@鋁,我很樂意嘗試幫助聊天:http://chat.stackoverflow.com/rooms/40543/ios-developer – jonahb

+0

@jonhab哇,非常感謝! – Aluminum