Skip to content

Instantly share code, notes, and snippets.

@zhangwc
Created August 28, 2013 16:01
Show Gist options
  • Save zhangwc/6367735 to your computer and use it in GitHub Desktop.
Save zhangwc/6367735 to your computer and use it in GitHub Desktop.
//把view截图为UIImage
- (UIImage *)renderInImage:(UIView *)view
{
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return (img);
}
//currentView截图转换为PDF
- (NSMutableData *)renderInView:(UIView *)currentView
{
NSMutableData *mtlData = [[[NSMutableData alloc] init] autorelease];
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)mtlData);
CGRect mediaBox = currentView.bounds;
CGContextRef ctx = CGPDFContextCreate(dataConsumer, &mediaBox, NULL);
CGPDFContextBeginPage(ctx, NULL);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -mediaBox.size.height);
[currentView.layer renderInContext:ctx];
CGPDFContextEndPage(ctx);
CFRelease(ctx);
CGDataConsumerRelease(dataConsumer);
return (mtlData);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment