IOS 8 Crash - renderInContext: UIGraphicsGetCurrentContext ()

Before iOS 8, I had no problem with this, and now yes.

LOG:

Assertion failed: (CGFloatIsValid(x) && CGFloatIsValid(y)), function void CGPathMoveToPoint(CGMutablePathRef, const CGAffineTransform *, CGFloat, CGFloat), file Paths/CGPath.cc, line 254. 

This is my code:

 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(); 

My goal is to convert the view into an image.

+5
source share
6 answers

Check for empty rectangles in what you draw, whether it be the view border or the rectangle of the layer content. I noticed that the assertion failed in iOS 8, where before, empty rectangles were silently ignored.

I added a few ...

 if (!CGRectIsEmpty(bounds)) { } 

... conditions in my drawing.

+2
source

We ran into this problem and tracked it to a point of view that had a cornerRadius parameter on CALayer but had zero size. In our case, this happened only on the device - not on the simulator. If you see _renderBorderInContext and CA_CGContextAddRoundRect in their opposite direction, then you probably see the same thing.

The zero size in any dimension (height / width) will cause this error if the radius of the angle is set. Unfortunately, since this statement does not allow us to catch the error and recover, we are therefore exploring the possibility of going through the hierarchy before the snapshot in order to detect the case and recover by setting cornerRadius to 0 and vice versa after calling renderInContext.

+1
source

For now ... I will allow the export in another way.

Sincerely.

 - (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]; } 
0
source

Works in 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]; } 
0
source

Before using with Core Graphics, check your float with isnan (x).

0
source

According to the answer from tyler, I am fixing the problem. You can just find the problematic view in self.view. Ios 8 does not allow you to view the zero size for the installation of cornerRadius. Thus, you should have zero size and set cornerRadius for it. You can run the following code to find out and fix it.

 - (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; } } 
0
source

Source: https://habr.com/ru/post/1207903/


All Articles