ios – 捕获UIView并另存为图像

前端之家收集整理的这篇文章主要介绍了ios – 捕获UIView并另存为图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
首先我在UI ImageView上添加UILable,然后在屏幕截图UIView之后,图像不正确地捕获UIView Frame我也附加了图像url.

1) Original Image Link :-

2) After Capture the image Link :-

代码: –

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 {

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

}

猜你在找的iOS相关文章