ios如何捕捉屏幕的特定部分
我想要捕捉iPhone屏幕的特定部分。 我用UIGraphicsBeginImageContextWithOptions
,但不能捕捉屏幕的一部分。 请帮帮我。
你可以使用UIGraphicsGetImageFromCurrentContext
截图。 从内存写下面的代码,所以错误可能。 请自行纠正。
- (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; }
你可以使用这个代码
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);
这是UIView
上的一个快速扩展,它将捕获视图中的整个视图或框架。 它考虑到屏幕比例。
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 } }
@Superdev的答案是正确的,我想添加的是一个小窍门,如果你想捕捉父视图,并避免子视图(特别是覆盖视图)你可以做的是使这个子视图属性,并使用以下
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;
它的一个小窍门,希望有人会觉得它有用
捕捉清晰截图的另一种方法;
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();
SWIFT 3.0 Xcode8版本来自@jDutton答案,为我工作。
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 } }
如果您崩溃并收到以下消息:
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.
试试这个解决scheme
希望节省您的时间!