我从image
内容中抓取UIView
并且工作正常但是当我使用iOS 10.3.3
在iPhone5S中运行时,捕获的image
仅包含黑视图。< / p>
-(UIImage *)captureImageFromView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
问题是什么? 在此先感谢。
答案 0 :(得分:1)
您可以按照@LGP的说明进行操作。
在如下所示的主线程上使用以下功能。
-(UIImage *)captureImageFromView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
主线程代码。
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *imgResult = [self captureImageFromView:yourView];
});
答案 1 :(得分:0)
请尝试使用drawViewHierarchyInRect
。如果这不起作用那么问题可能是您捕获了错误的视图或者以某种方式更改了它之后。
-(UIImage *)captureImageFromView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
答案 2 :(得分:-1)
FOR SWIFT
func snapshot() -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, true, UIScreen.main.scale)
self.view.layer.render(in: UIGraphicsGetCurrentContext()!)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img!
}
FOR OBJECTIVE C
- (UIImage *)captureView {
UIGraphicsBeginImageContext(yourview.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextFillRect(ctx, yourview.frame);
[yourview.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(@"size %f %f",newImage.size.height,newImage.size.width);
return newImage;
}
如果您需要,则在调用隐藏按钮等之前,也不会在图像上显示。