如何捕获 UIView 顶部 UIView [英] Howe to capture UIView top UIView
问题描述
我有 UIView
显示图形..现在我需要将它捕获到 UIImage
并且我用谷歌搜索它并得到以下代码.但是如果我使用它在我的代码中它不起作用.即使我使用断点编译器也无法达到此目的.我在我的 UIView
中使用它.我哪里出错了?
i have UIView
which displays the Graph..now I need to capture it to the UIImage
and I googled it and got the below code.but if i use this in my code its not work.even if I use breakpoint compiler does not reach to this.I am using this in my UIView
.where I am going wrong?
+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
推荐答案
您可以使用以下方法对iPhone应用程序的任何视图或整个屏幕进行截图
You can take a screenshot of any view or whole screen of the iPhone app with below method
- (UIImage *)captureView {
//hide controls if needed
CGRect rect = [self.view bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
然后像下面这样称呼它...
And call it like bellow...
UIImage *tempImageSave=[self captureView];
你也可以用这个波纹管保存这张图片作为相册..
and you can also save this image with this bellow line for photo album..
UIImageWriteToSavedPhotosAlbum(tempImageSave,nil,nil,nil);
您还可以使用此波纹管将此图像保存为文档目录..
and you can also save this image with this bellow line for Document Directory..
NSData *imageData = UIImagePNGRepresentation(tempImageSave);
NSFileManager *fileMan = [NSFileManager defaultManager];
NSString *fileName = [NSString stringWithFormat:@"%d.png",1];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
[fileMan createFileAtPath:pdfFileName contents:imageData attributes:nil];
如果视图包含图层图像或一些与图形相关的数据,则使用以下方法.
If the view contains the layer images or some graphics related data then use below method.
-(UIImage *)convertViewToImage:(UIView *)viewTemp
{
UIGraphicsBeginImageContext(viewTemp.bounds.size);
[viewTemp drawViewHierarchyInRect:viewTemp.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
并使用如下方法.
UIImage *tempImageSave = [self convertViewToImage:yourView];
希望对你有帮助.
这篇关于如何捕获 UIView 顶部 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!