iOS表格绘制

iOS PDF转为相册图片

2019-08-16  本文已影响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(@"失败");

    }

}

上一篇下一篇

猜你喜欢

热点阅读