在判斷用戶是否具有訪問相冊權(quán)限的時(shí)候,需要導(dǎo)入頭文件#import<Photos/Photos.h>、#import <AssetsLibrary/AssetsLibrary.h>、#import<CoreLocation/CLLocationManager.h>
#pragma mark---保存相冊
- (void)didSave{
? ? if ([self isOrUsePhotos]) {
? ? ? ? //有權(quán)限
? ? ? ? [self loadImageFinished:[self loadImage]];
? ? }else{
? ? ? ? //無權(quán)限----提示用戶--設(shè)置--去開啟允許
? ? }
}
//訪問權(quán)限判斷
- (BOOL)isOrUsePhotos{
? ? if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
? ? ? ? ALAuthorizationStatus author =[ALAssetsLibrary authorizationStatus];
? ? ? ? if (author == kCLAuthorizationStatusRestricted || author == kCLAuthorizationStatusDenied) {
? ? ? ? ? ? //無權(quán)限
? ? ? ? ? ? returnNO;
? ? ? ? }
? ? }
? ? else{
? ? ? ? PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
? ? ? ? if (status == PHAuthorizationStatusRestricted ||
? ? ? ? ? ? status ==PHAuthorizationStatusDenied) {
? ? ? ? ? ? //無權(quán)限
? ? ? ? ? ? returnNO;
? ? ? ? }
? ? }
? ? return YES;
}
//轉(zhuǎn)化為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轉(zhuǎn)化為PDF的DATA數(shù)據(jù)
- (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;
}