美文网首页iOS表格绘制
iOS PDF转为相册图片

iOS PDF转为相册图片

作者: 阿栋先森 | 来源:发表于2019-08-16 17:27 被阅读0次

    //要跟据retina 屏幕做分辨率适配

    -(void)getUIImageFromPDFPage:(int)page_number filePath:(NSString*)pdfPath

    {

        NSURL*filePath = [NSURLfileURLWithPath:pdfPath];

        //读取PDF原文件的大小

        CGPDFDocumentRef doc = CGPDFDocumentCreateWithURL((__bridge CFURLRef)filePath);

        CGPDFPageRefpage =CGPDFDocumentGetPage(doc,1);

        CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);

        pageRect.origin=CGPointZero;

        pageRect.size.height= pageRect.size.height*2;

        pageRect.size.width= pageRect.size.width*2;

        //开启图片绘制 上下文

        UIGraphicsBeginImageContext(pageRect.size);

        CGContextRef  context =UIGraphicsGetCurrentContext();

        // 设置白色背景

        CGContextSetRGBFillColor(context,1.0,1.0,1.0,1.0);

        CGContextFillRect(context,pageRect);

        CGContextSaveGState(context);

        //进行翻转

        CGContextTranslateCTM(context, -pageRect.size.width/2, pageRect.size.height*1.5);

        CGContextScaleCTM(context,2, -2);

        CGContextSetInterpolationQuality(context, kCGInterpolationHigh);

        CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);

        CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, pageRect,0,true));

        CGContextDrawPDFPage(context,page);

        CGContextRestoreGState(context);

        UIImage *pdfImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        [selfsaveImage:pdfImage];

    }

    - (void)saveImage:(UIImage*)image {

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

    }

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

    {

        if(!error) {

            NSLog(@"成功");

        }

        else{

            NSLog(@"失败");

        }

    }

    相关文章

      网友评论

        本文标题:iOS PDF转为相册图片

        本文链接:https://www.haomeiwen.com/subject/jrvqsctx.html