捕获UIView并另存为图像 [英] Capture UIView and Save as Image

查看:100
本文介绍了捕获UIView并另存为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我在 UIImageView 上添加 UILable ,然后我截取 UIView ,图片不正确捕获 UIView 框架我还附上图片网址。







代码:

  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];


解决方案

您可以截取任何<$ c $的屏幕截图c> UIView 或捕获任何 UIView 并将其保存为带有以下代码的图像。

   - (UIImage *)captureView {

//如果需要隐藏控件
CGRect rect = [self.view bounds];

UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;

}


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.

1) Original Image Link:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆