Capture UIView and Save as Image

2019-01-10 23:35发布

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.

enter image description here

1) Original Image Link:

enter image description here

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

6条回答
叛逆
2楼-- · 2019-01-10 23:55

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;

}
查看更多
你好瞎i
3楼-- · 2019-01-10 23:55

Take snapshot of a UIView

pass your view to this function it will handle everything itself( No need to set bounds or frames)

- (UIImage *)takeSnapshotOfView:(UIView *)view
{
    UIGraphicsBeginImageContext(CGSizeMake(view.frame.size.width, view.frame.size.height));
    [view drawViewHierarchyInRect:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height) afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}
查看更多
相关推荐>>
4楼-- · 2019-01-10 23:59

Try this if you want captured image maintain its original quality.

-(UIImage*)captureViewWithAllSubView:(UIView*)view{
   UIGraphicsBeginImageContextWithOptions(view.bounds.size, 
   view.opaque, 0.0f);
   [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
   UIImage *capturedImage = 
   UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   return capturedImage;}
查看更多
The star\"
5楼-- · 2019-01-11 00:06

Try like this,

UIGraphicsBeginImageContext(viewImage.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, viewImage.frame);     

[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];
查看更多
趁早两清
6楼-- · 2019-01-11 00:15

Swift 3 Version:

func takeSnapshotOfView(view:UIView) -> UIImage? {
        UIGraphicsBeginImageContext(CGSize(width: view.frame.size.width, height: view.frame.size.height))
        view.drawHierarchy(in: CGRect(x: 0.0, y: 0.0, width: view.frame.size.width, height: view.frame.size.height), afterScreenUpdates: true)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
查看更多
ゆ 、 Hurt°
7楼-- · 2019-01-11 00:19

you can use the below code to

UIGraphicsBeginImageContext(pictureView.bounds.size); 
                    

[pictureView.layer renderInContext:UIGraphicsGetCurrentContext()]; 

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
查看更多
登录 后发表回答