iOS 8崩溃-RenderContext:UIGraphicsGetCurrentContext()
在iOS 8之前,我对此没有任何问题&现在,是的 日志: 这是我的代码:iOS 8崩溃-RenderContext:UIGraphicsGetCurrentContext(),ios,objective-c,ios8,core-graphics,Ios,Objective C,Ios8,Core Graphics,在iOS 8之前,我对此没有任何问题&现在,是的 日志: 这是我的代码: UIImage* image = nil; CGSize imageSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height); UIGraphicsBeginImageContextWithOptions(imageSize, NO , 0.0f); [self.view.layer renderInContext:UIG
UIImage* image = nil;
CGSize imageSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
UIGraphicsBeginImageContextWithOptions(imageSize, NO , 0.0f);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; // <- ERROR
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage*image=nil;
CGSize imageSize=CGSizeMake(self.view.bounds.size.width,self.view.bounds.size.height);
UIGraphicsBeginImageContextWithOptions(图像大小,编号,0.0f);
[self.view.layer renderContext:UIGraphicsGetCurrentContext()];// 检查正在绘制的内容中是否有空矩形,无论是视图的边界还是图层的内容矩形。我注意到ios8上的断言失败,在此之前,空矩形被默默忽略
我添加了一些
if (!CGRectIsEmpty(bounds)) {
}
…我的绘图中的条件。而。。。我将以其他方式解决出口问题
问候
- (IBAction)ibaExportar:(id)sender {
NSString *mystr = @"";
NSString *csvstr;
csvstr = [NSString stringWithFormat:@",Cliente,Domicilio,Dueño"];
mystr = [NSString stringWithFormat:@"%@,%@,%@\n",self.numCliente,self.iboDomicilio.text,self.iboDueno.text];
csvstr = [NSString stringWithFormat:@"%@\n%@",csvstr,mystr];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
fileName = [docDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Reporte.csv"]];
NSError *error = NULL;
BOOL written = [csvstr writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (!written)
NSLog(@"write failed, error=%@", error);
else{
[self sendEmail];
}
}
- (void) sendEmail {
NSString*subject;
subject= [@"Reporte Cliente " stringByAppendingString:@""];
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:subject];
NSData *dataFile = [NSData dataWithContentsOfFile:fileName];
[picker addAttachmentData:dataFile mimeType:@"text/csv" fileName:@"Reporte.csv"];
NSString *emailBody =subject;
[picker setMessageBody:emailBody isHTML:NO];
[self presentViewController:picker animated:YES completion:nil];
}
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[self dismissViewControllerAnimated:YES completion:nil];
}
在IOS 8中工作
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
fileName = [docDir stringByAppendingPathComponent:[NSString stringWithFormat:@"reporte.png"]];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
NSError *error = NULL;
BOOL written =[imageData writeToFile:fileName atomically:YES];
if (!written)
NSLog(@"write failed, error=%@", error);
else{
[self sendPorCorreo];
}
我们也遇到了这个问题,并跟踪到一个视图,该视图在CALayer上设置了一个cornerRadius,但大小为零。在我们的例子中,这只发生在设备上,而不是模拟器上。如果您在回溯中看到_renderBorderInContext和CA_CGContextAddRoundRect,那么您可能看到了相同的情况
如果设置了角半径,则任一尺寸(高度/宽度)中的零尺寸将导致出现此错误。不幸的是,由于这是一个断言,因此不可能捕获错误并进行恢复,因此我们正在探索在快照之前遍历层次结构的选项,以便通过将cornerRadius设置为0并在调用renderInContext后返回来检测案例并进行恢复 在与Core Graphics一起使用之前,请使用isnan(x)检查您的浮点。根据tyler的回答,我解决了这个问题。您可以在self.view中找到有问题的视图。ios 8不允许零尺寸视图设置转弯半径。因此,您必须拥有零尺寸视图,并为其设置拐角半径。您可以运行以下代码来查找并修复它
- (void)findZeroSizeControlWithSuperView:(UIView *)superView {
[self isProblematicCrotrol:superView];
[superView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self isProblematicCrotrol:obj];
}];
}
- (BOOL)isProblematicCrotrol:(UIView *)view {
if ((view.frame.size.width == 0 || view.frame.size.height == 0) && view.layer.cornerRadius != 0) {
NSLog(@"this is the problematic view:%@", view);
return YES;
} else {
return NO;
}
}
嗨,菲利普,噢!好啊谢谢。我试过了,但没有成功,这段代码是在UIGraphicsBeginImageContextWithOptions之后编写的?如果(!CGRectIsEmpty(self.view.bounds)){[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];}确定,那么层呢?它有一个界限和一个内容,这可能是一个问题。我不知道。当我看到错误时,是因为一个空矩形。如果你说你的不是空的,我想一定是其他原因。好吧!谢谢大家!@菲利普。是的,没有空矩形,我将用其他方法解决导出问题。同时,我将导出到excel(CSV)。另外一点信息-如果视图被隐藏或具有alpha 0,则断言不会跳闸。因此,如果视图的拐角半径、高度或宽度不为零,且未隐藏且alpha值不为零,请注意renderInContext。
- (void)findZeroSizeControlWithSuperView:(UIView *)superView {
[self isProblematicCrotrol:superView];
[superView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self isProblematicCrotrol:obj];
}];
}
- (BOOL)isProblematicCrotrol:(UIView *)view {
if ((view.frame.size.width == 0 || view.frame.size.height == 0) && view.layer.cornerRadius != 0) {
NSLog(@"this is the problematic view:%@", view);
return YES;
} else {
return NO;
}
}