捕获 UIView 并另存为图像 [英] Capture UIView and Save as Image
本文介绍了捕获 UIView 并另存为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
首先我在 UIImageView
上添加 UILable
然后在我截取 UIView
之后,图像不能正确捕获 UIView
Frame 我还附上了图片网址.
代码:
UIGraphicsBeginImageContext(viewImage.frame.size);[viewImage.layer renderInContext:UIGraphicsGetCurrentContext()];UIImage *vwImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();NSData *data=UIImagePNGRepresentation(vwImage);NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = [paths objectAtIndex:0];//NSString *imgName = [NSString stringWithFormat:imagename];NSString *strPath = [documentsDirectory stringByAppendingPathComponent:imagename];[data writeToFile:strPath atomically:YES];
解决方案
您可以截取任何 UIView
的屏幕截图或捕获任何 UIView
并将其保存为图像下面的代码.
- (UIImage *)captureView {//如果需要隐藏控件CGRect rect = [self.view bounds];UIGraphicsBeginImageContext(rect.size);CGContextRef 上下文 = UIGraphicsGetCurrentContext();[self.view.layer renderInContext:context];UIImage *img = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();返回图像;}
First of i Add the UILable
on UIImageView
and then after i screenshot the UIView
, the image not proper capture the UIView
Frame i also attach the image url.
2) After Capture the image Link:
Code:
UIGraphicsBeginImageContext(viewImage.frame.size);
[viewImage.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *vwImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data=UIImagePNGRepresentation(vwImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// NSString *imgName = [NSString stringWithFormat:imagename];
NSString *strPath = [documentsDirectory stringByAppendingPathComponent:imagename];
[data writeToFile:strPath atomically:YES];
解决方案
You can take the screenshots of any UIView
or capture any UIView
and save it as an image with below code.
- (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;
}
这篇关于捕获 UIView 并另存为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文