Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture UIView and Save as Image

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];
like image 985
Rushabh Avatar asked Jun 17 '13 10:06

Rushabh


People also ask

How do I render UIView to UIImage?

extension UIView { // Using a function since `var image` might conflict with an existing variable // (like on `UIImageView`) func asImage() -> UIImage { if #available(iOS 10.0, *) { let renderer = UIGraphicsImageRenderer(bounds: bounds) return renderer. image { rendererContext in layer. render(in: rendererContext.

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

What is UIImageView?

An object that manages image data in your app.

How do I print UIImage?

If you have a UIImage you want to print, you can just pass it in. If you want to print text, you can wrap it inside an NSAttributedString with some formatting, then place that inside a UISimpleTextPrintFormatter object, then print that – iOS automatically takes care of pagination, margins and more.


2 Answers

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;

}
like image 174
Paras Joshi Avatar answered Sep 22 '22 02:09

Paras Joshi


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;
}
like image 30
Zar E Ahmer Avatar answered Sep 22 '22 02:09

Zar E Ahmer