iOS--截取webView保存在相冊和存儲為PDF

在判斷用戶是否具有訪問相冊權限的時候,需要導入頭文件#import<Photos/Photos.h>、#import <AssetsLibrary/AssetsLibrary.h>、#import<CoreLocation/CLLocationManager.h>


#pragma mark---保存相冊

- (void)didSave{

? ? if ([self isOrUsePhotos]) {

? ? ? ? //有權限

? ? ? ? [self loadImageFinished:[self loadImage]];

? ? }else{

? ? ? ? //無權限----提示用戶--設置--去開啟允許

? ? }


}

//訪問權限判斷

- (BOOL)isOrUsePhotos{

? ? if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {

? ? ? ? ALAuthorizationStatus author =[ALAssetsLibrary authorizationStatus];

? ? ? ? if (author == kCLAuthorizationStatusRestricted || author == kCLAuthorizationStatusDenied) {

? ? ? ? ? ? //無權限

? ? ? ? ? ? returnNO;

? ? ? ? }

? ? }

? ? else{

? ? ? ? PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];

? ? ? ? if (status == PHAuthorizationStatusRestricted ||

? ? ? ? ? ? status ==PHAuthorizationStatusDenied) {

? ? ? ? ? ? //無權限

? ? ? ? ? ? returnNO;

? ? ? ? }

? ? }

? ? return YES;

}

//轉化為PDF保存在本地

- (void)savePDF{

?? ? NSData*pdfData = [selfconverToPDF];

? ? NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/testFile.pdf"]];

? ? BOOLresult = [pdfDatawriteToFile:pathatomically:YES];

? ? if(result) {


? ? ? ? NSLog(@"保存成功");

? ? ? ? UIAlertController *alertCtrl = [UIAlertController alertControllerWithTitle:@"溫馨提示" message:@"保存成功" preferredStyle:UIAlertControllerStyleAlert];

? ? ? ? UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

? ? ? ? }];


? ? ? ? UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

? ? ? ? [alertCtrladdAction:action1];

? ? ? ? [alertCtrladdAction:action2];

? ? ? ? [self presentViewController:alertCtrl animated:YES completion:nil];

? ? }else{

? ? ? ? NSLog(@"保存失敗");

? ? }


}


- (void)loadImageFinished:(UIImage*)image

{

? ? UIImageWriteToSavedPhotosAlbum(image,self,@selector(image:didFinishSavingWithError:contextInfo:), (__bridgevoid*)self);

}

- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo

{

? ? NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);

}


#pragma mark---圖片保存到相冊

- (UIImage*)loadImage

{

CGSizeboundsSize =_myWebView.bounds.size;

CGFloat boundsWidth = _myWebView.bounds.size.width;

CGFloat boundsHeight = _myWebView.bounds.size.height;

CGPoint offset = _myWebView.scrollView.contentOffset;

[_myWebView.scrollView setContentOffset:CGPointMake(0, 0)];

CGFloat contentHeight = _myWebView.scrollView.contentSize.height;

NSMutableArray *images = [NSMutableArray array];

while(contentHeight >0) {

? ? UIGraphicsBeginImageContext(boundsSize);


? ? [_myWebView.layer renderInContext:UIGraphicsGetCurrentContext()];


? ? UIImage *image = UIGraphicsGetImageFromCurrentImageContext();


? ? UIGraphicsEndImageContext();


? ? [imagesaddObject:image];


? ? CGFloat offsetY = _myWebView.scrollView.contentOffset.y;


? ? [_myWebView.scrollView setContentOffset:CGPointMake(0, offsetY + boundsHeight)];


? ? contentHeight -= boundsHeight;

}

[_myWebView.scrollView setContentOffset:offset];

UIGraphicsBeginImageContext(_myWebView.scrollView.contentSize);

[images enumerateObjectsUsingBlock:^(UIImage *image, NSUInteger idx, BOOL *stop) {

? ? [imagedrawInRect:CGRectMake(0, boundsHeight * idx, boundsWidth, boundsHeight)];

}];

UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

returnfullImage;

}

#pragma mark---將_myWebView轉化為PDF的DATA數據

- (NSData*)converToPDF{

? ? UIViewPrintFormatter *fmt = [_myWebView viewPrintFormatter];

? ? UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init];

? ? [renderaddPrintFormatter:fmt startingAtPageAtIndex:0];

? ? CGRectpage;

? ? page.origin.x=0;

? ? page.origin.y=0;

? ? page.size.width=600;

? ? page.size.height=768;

? ? CGRectprintable=CGRectInset( page,50,50);

? ? [rendersetValue:[NSValue valueWithCGRect:page] forKey:@"paperRect"];

? ? [rendersetValue:[NSValue valueWithCGRect:printable] forKey:@"printableRect"];

? ? NSMutableData * pdfData = [NSMutableData data];

? ? UIGraphicsBeginPDFContextToData( pdfData, CGRectZero, nil );


? ? for(NSIntegeri=0; i < [rendernumberOfPages]; i++)

? ? {

? ? ? ? UIGraphicsBeginPDFPage();

? ? ? ? CGRect bounds = UIGraphicsGetPDFContextBounds();

? ? ? ? [renderdrawPageAtIndex:iinRect:bounds];

? ? }

?? UIGraphicsEndPDFContext();

? ? returnpdfData;

}

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 之前考了張語文卷子,粗心看漏題得了個破分數,作文寫跑題了。作文又是高中的孔子說孫子說你同意誰,我一個初中生,次次初...
    折子木閱讀 175評論 0 1
  • 鑒于很多HR還是基本會將簡歷全部看一遍,秒殺一些,而哪些類型的簡歷將被直接秒殺?下面就為大家揭曉。 沒有實踐經歷的...
    職問師姐閱讀 985評論 0 1