2011-06-29 5 views
0

私のアプリケーションではmapviewを使用しました。特定の場所で私は注釈を入れることができます。私はその場所のスナップショットを注釈付きで撮りたいです。マップビューからスクリーンショットを取る方法

私はこれを実装しようとしました。

CGSize size = self.view.bounds.size; 
CGRect screensize = CGRectMake(40,40,size.width-240,size.height-400); 
UIGraphicsBeginImageContext(screensize.size); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageWriteToSavedPhotosAlbum(screenshotImage, nil, nil, nil); 

UIGraphicsEndImageContext(); 

このコードはスナップを取りますが、(0,0)座標からスナップをとります。私は(40,40)座標からスナップを取りたいと思って、画像のサイズは80 * 80でなければなりません。

どうすればいいですか?

答えて

1

私はあなたがそれを直接行うことはできないと思います。画像全体を取得し、目的の領域を切り抜きます。

CGSize size = self.view.bounds.size; 
CGRect cropRect = CGRectMake(40, 40, 80, 80); 

/* Get the entire on screen map as Image */ 
UIGraphicsBeginImageContext(size); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage * mapImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

/* Crop the desired region */ 
CGImageRef imageRef = CGImageCreateWithImageInRect(mapImage.CGImage, cropRect); 
UIImage * cropImage = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef); 

/* Save the cropped image */ 
UIImageWriteToSavedPhotosAlbum(cropImage, nil, nil, nil); 

UIGraphicsEndImageContext(); 
+0

このコードは機能します。ありがとう。 –

関連する問題