Reputation: 660
Before iOS 8, I didn't have problems with this & 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 purpose is to convert the view to image.
Upvotes: 5
Views: 3737
Reputation: 1
According to the answer from tyler, I fix the problem. You can just find out the problematic view in self.view. The ios 8 not allow the zero size view to set the cornerRadius. So you must have a zero size view and set the cornerRadius for it. You can run the following code to find it 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;
}
}
Upvotes: 0
Reputation: 4919
Check your float with isnan(x) before using with Core Graphics.
Upvotes: 0
Reputation: 2925
We ran into this problem as well and tracked it down to a view that had a cornerRadius set on the CALayer, but had a zero size. In our case, this was only occurring on a device - not on the simulator. If you see _renderBorderInContext and CA_CGContextAddRoundRect in your backtrace then you're probably seeing the same thing.
A zero size in either dimension (height/width) will cause this error to occur if a corner radius is set. Unfortunately since it's an assertion it's not possible to catch the error and recover, so we're exploring the option of traversing the hierarchy prior to snapshotting to detect the case and recover by setting the cornerRadius to 0 and back after the call to renderInContext.
Upvotes: 1
Reputation: 660
While... I will solve the export of other way.
Regards.
- (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];
}
Upvotes: 0
Reputation: 31026
Check for empty rectangles in what you're drawing, whether the view's bounds or the layer's content rectangle. I have noticed that assertion failure on iOS 8 where, before, empty rectangles were silently ignored.
I've added a number of...
if (!CGRectIsEmpty(bounds)) {
}
...conditions in my drawing.
Upvotes: 2
Reputation: 660
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];
}
Upvotes: 0