iOS--截取webView保存在相册和存储为PDF

在判断用户是否具有访问相册权限的时候,需要导入头文件#import、#import 、#import


#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;

}

你可能感兴趣的:(iOS--截取webView保存在相册和存储为PDF)