这是我的视图层次结构:我在 Xib 中有一个视图(默认 viewControllher 的 _view),其中有一个 UIWebView 子视图。在同一个 Xib 文件中,另一个名为 floatView 的单独的第二个视图(不在 _view 内)。floatView 以编程方式添加到 UIWebView 之上,并且可以由用户拖动。在 floatView 我有一个较小的 subView (cropView)。我想使用cropView 作为裁剪框来获取它下面的可见部分webView。
这是我尝试使用的代码,但结果不是我需要的。
- (IBAction)captureImage:(id)sender {
CGSize layerSize = self.view.bounds.size;
UIGraphicsBeginImageContext(layerSize);
[self.webView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
[floatView convertRect:cropView.frame toView:self.view];
CGRect CropFrame = cropView.frame;
CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, CropFrame);
UIImage *subViewImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(subViewImage, nil, nil, nil);
}
我在相机胶卷中获得的图像是我裁剪的图像,尺寸正确,不在可见 webView 的正确位置。
我查看了其他帖子和解决方案,但找不到可以解决我的问题的帖子和解决方案。