IOS how to capture a specific part of the screen

I want to capture a specific part of the iPhone screen. I used UIGraphicsBeginImageContextWithOptions but could not capture part of the screen with this. Please help me.

+7
source share
6 answers

You can take a screenshot using UIGraphicsGetImageFromCurrentContext . Wrote the code below from memory, so errors are possible. Please fix it yourself.

 - (UIImage*)captureView:(UIView *)yourView { CGRect rect = [[UIScreen mainScreen] bounds]; UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); [yourView.layer renderInContext:context]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } 
+15
source

you can use this code

 UIGraphicsBeginImageContext(self.view.bounds.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); CGRect rect = CGRectMake(250,61 ,410, 255); CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect); UIImage *img = [UIImage imageWithCGImage:imageRef]; UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil); CGImageRelease(imageRef); 
+25
source

Here's a quick extension to UIView that will either capture the whole view or frame in the view. It takes into account the scale of the screen.

 extension UIView { func imageSnapshot() -> UIImage { return self.imageSnapshotCroppedToFrame(nil) } func imageSnapshotCroppedToFrame(frame: CGRect?) -> UIImage { let scaleFactor = UIScreen.mainScreen().scale UIGraphicsBeginImageContextWithOptions(bounds.size, false, scaleFactor) self.drawViewHierarchyInRect(bounds, afterScreenUpdates: true) var image: UIImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() if let frame = frame { // UIImages are measured in points, but CGImages are measured in pixels let scaledRect = CGRectApplyAffineTransform(frame, CGAffineTransformMakeScale(scaleFactor, scaleFactor)) if let imageRef = CGImageCreateWithImageInRect(image.CGImage, scaledRect) { image = UIImage(CGImage: imageRef) } } return image } } 
+3
source

the answer by @Superdev right on the spot, what I want to add is a little trick if you want to grab the parent view and avoid subqueries (particularly the overlay view), what you can do is make this a subview property and use the following

 CGFloat width = CGRectGetWidth(self.view.bounds); CGFloat height = CGRectGetHeight(self.view.bounds); _overlayView.hidden = YES; UIGraphicsBeginImageContext(self.view.bounds.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); CGRect rect = CGRectMake(0,0 ,width, height); CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect); UIImage *img = [UIImage imageWithCGImage:imageRef]; UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil); CGImageRelease(imageRef); _overlayView.hidden = NO; 

his little trick, hope someone finds it useful

+1
source

Another way to capture a clear screenshot;

 if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) UIGraphicsBeginImageContextWithOptions(self.captureView.frame.size, NO, [UIScreen mainScreen].scale); else UIGraphicsBeginImageContext(self.captureView.frame.size); [self.captureView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); [viewImage drawInRect:CGRectMake(0.0, 0.0, 640,960)]; //NSData*m_Imgdata=UIImagePNGRepresentation(viewImage); NSData*m_Imgdata=UIImageJPEGRepresentation(viewImage, 1.0); // UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); UIGraphicsEndImageContext(); 
0
source

version SWIFT 3.0 Xcode8 came from @jDutton answer and work for me.

 extension UIView { func imageSnapshot() -> UIImage { return self.imageSnapshotCroppedToFrame(frame: nil) } func imageSnapshotCroppedToFrame(frame: CGRect?) -> UIImage { let scaleFactor = UIScreen.main.scale UIGraphicsBeginImageContextWithOptions(bounds.size, false, scaleFactor) self.drawHierarchy(in: bounds, afterScreenUpdates: true) var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext() if let frame = frame { // UIImages are measured in points, but CGImages are measured in pixels let scaledRect = frame.applying(CGAffineTransform(scaleX: scaleFactor, y: scaleFactor)) if let imageRef = image.cgImage!.cropping(to: scaledRect) { image = UIImage(cgImage: imageRef) } } return image } } 

if you have failed and received the following message:

 GContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 

try this solution

Hope to save your time!

0
source

All Articles