獲取tableView截取的多張圖片
-(NSArray *)imagesArrForTableView{
//0.更新historyTableView的約束 選擇學科的不能重復截圖
[self.historyTalbelView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.view.mas_top);
}];
// 1.獲取WebView的寬高
CGSize boundsSize = self.historyTalbelView.bounds.size;
//CGFloat boundsWidth = boundsSize.width;
CGFloat boundsHeight = boundsSize.height;
// 2.獲取contentSize
CGSize contentSize = self.historyTalbelView.contentSize;
CGFloat contentHeight = contentSize.height;
// 3.保存原始偏移量,便于截圖后復位
CGPoint offset = self.historyTalbelView.contentOffset;
// 4.設定最初的偏移量為(0,0);
[self.historyTalbelView setContentOffset:CGPointMake(0,0)];
NSMutableArray *images = [NSMutableArray array];
while (contentHeight > 0) {
// 5.獲取CGContext 5.獲取CGContext
if (contentHeight - boundsSize.height < 0) {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(boundsSize.width, boundsSize.height-80), NO, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 6.渲染要截取的區域
[self.view.layer renderInContext:ctx];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// 7.截取的圖片保存起來
[images addObject:image];
}else{
UIGraphicsBeginImageContextWithOptions(boundsSize, NO, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 6.渲染要截取的區域
[self.view.layer renderInContext:ctx];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// 7.截取的圖片保存起來
[images addObject:image];
}
CGFloat offsetY = self.historyTalbelView.contentOffset.y;
[self.historyTalbelView setContentOffset:CGPointMake(0, offsetY + boundsHeight)];
contentHeight -= boundsHeight;
}
// 8 tableView 恢復到之前的顯示區域
[self.historyTalbelView setContentOffset:offset];
// CGFloat scale = [UIScreen mainScreen].scale;
// CGSize imageSize = CGSizeMake(contentSize.width * scale,
// (contentSize.height-80) * scale);
// 9.根據設備的解析度重新繪制、拼接成完整清晰圖片
// UIGraphicsBeginImageContext(imageSize);
// [images enumerateObjectsUsingBlock:^(UIImage *image, NSUInteger idx, BOOL *stop) {
// [image drawInRect:CGRectMake(0,scale * boundsHeight * idx,scale * image.size.width,scale * image.size.height)];
// }];
// UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext(); // 用這個方法是獲取完整的tableView圖片;
// UIGraphicsEndImageContext();
//10.更新historyTableView的約束
[self.historyTalbelView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.view.mas_top).offset(64);
}];
return images;
}
把多張圖片轉成PDF檔案
- (NSString *)createPDF:(NSArray *)dataSource{
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",LOCAL_ERROROBOOK_PATH,self.vipId]];
NSString *pdfPath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@", [self getFileName]]];
if ([self creatFile:path]) {
NSLog(@" --- 創建PDF路徑成功 --- ");
}
// CGRectZero 表示默認尺寸,引數可修改,設定自己需要的尺寸
UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, NULL);
CGRect pdfBounds = UIGraphicsGetPDFContextBounds();
CGFloat pdfWidth = pdfBounds.size.width;
CGFloat pdfHeight = pdfBounds.size.height;
for (UIImage *image in dataSource) { // NSData * imageData in dataSource
//UIImage *image = [UIImage imageWithData:imageData];
// 繪制PDF
UIGraphicsBeginPDFPage();
CGFloat imageW = image.size.width;
CGFloat imageH = image.size.height;
if (imageW <= pdfWidth && imageH <= pdfHeight)
{
CGFloat originX = (pdfWidth - imageW) / 2;
CGFloat originY = (pdfHeight - imageH) / 2;
[image drawInRect:CGRectMake(originX, originY, imageW, imageH)];
}
else
{
CGFloat width,height;
if ((imageW / imageH) > (pdfWidth / pdfHeight))
{
width = pdfWidth;
height = width * imageH / imageW;
}
else
{
height = pdfHeight;
width = height * imageW / imageH;
}
[image drawInRect:CGRectMake((pdfWidth - width) / 2, (pdfHeight - height) / 2, width, height)];
}
}
UIGraphicsEndPDFContext();
return pdfPath;
}
/**
* 檔案名 2019.06.18 16:47-錯誤原題
*/
-(NSString *)getFileName{
NSString *dateString = [NSDate getDateStringByDate:[NSDate date]];
NSString *timeString = [NSDate getTimeWithSecondStringByDate:[NSDate date]];
NSString *typeString = @"WeekStudyReport";
NSString *fullFileName = [NSString stringWithFormat:@"%@ %@-%@-%@.pdf",dateString,timeString,typeString,self.selectSubjectName];
return fullFileName;
}
/**
* 創建檔案路徑
*/
-(BOOL)creatFile:(NSString*)filePath{
if (filePath.length==0) {
return NO;
}
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath]) {
return YES;
}
NSError *error;
BOOL isSuccess = [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error];
if (error) {
NSLog(@"creat File Failed:%@",[error localizedDescription]);
}
if (!isSuccess) {
return isSuccess;
}
isSuccess = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
return isSuccess;
}
相關文章參考:
將UIWebView顯示的內容轉為圖片和PDF
將UIWebView分屏截取,然后將截取的圖片拼接成一張圖片,
- (UIImage *)imageRepresentation{
CGSize boundsSize = self.bounds.size;
CGFloat boundsWidth = self.bounds.size.width;
CGFloat boundsHeight = self.bounds.size.height;
CGPoint offset = self.scrollView.contentOffset;
[self.scrollView setContentOffset:CGPointMake(0, 0)];
CGFloat contentHeight = self.scrollView.contentSize.height;
NSMutableArray *images = [NSMutableArray array];
while (contentHeight > 0) {
UIGraphicsBeginImageContext(boundsSize);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[images addObject:image];
CGFloat offsetY = self.scrollView.contentOffset.y;
[self.scrollView setContentOffset:CGPointMake(0, offsetY + boundsHeight)];
contentHeight -= boundsHeight;
}
[self.scrollView setContentOffset:offset];
UIGraphicsBeginImageContext(self.scrollView.contentSize);
[images enumerateObjectsUsingBlock:^(UIImage *image, NSUInteger idx, BOOL *stop) {
[image drawInRect:CGRectMake(0, boundsHeight * idx, boundsWidth, boundsHeight)];
}];
UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return fullImage;
}
將UIWebView從頭,contentOffset = (0, 0),開始截取webView.bounds.size.height高度的圖片,然后將_webView可見區域下移繼續截屏,這樣將所有截取的圖片按照順序拼接,就能得到整個UIWebView顯示內容的完整圖片,
- (NSData *)PDFData{
UIViewPrintFormatter *fmt = [self viewPrintFormatter];
UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init];
[render addPrintFormatter:fmt startingAtPageAtIndex:0];
CGRect page;
page.origin.x=0;
page.origin.y=0;
page.size.width=600;
page.size.height=768;
CGRect printable=CGRectInset( page, 50, 50 );
[render setValue:[NSValue valueWithCGRect:page] forKey:@"paperRect"];
[render setValue:[NSValue valueWithCGRect:printable] forKey:@"printableRect"];
NSMutableData * pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData( pdfData, CGRectZero, nil );
for (NSInteger i=0; i < [render numberOfPages]; i++)
{
UIGraphicsBeginPDFPage();
CGRect bounds = UIGraphicsGetPDFContextBounds();
[render drawPageAtIndex:i inRect:bounds];
}
UIGraphicsEndPDFContext();
return pdfData;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/247756.html
標籤:其他
上一篇:iOS電子簽名上篇【核心原理: 旋轉特定的螢屏】應用場景:采集電子簽名,支持簽名界面為橫屏其余頁面都是豎屏、清除重寫、靈活控制提示語資訊、以及查看商戶協議等
