在我的应用程序中,我使用了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。
我该怎么做?
答案 0 :(得分: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();