PDF 图片

//
//  DSPDFController.h
//  socketConnect3
//
//  Created by dascomzz on 16/1/27.
//  Copyright © 2016年 DS. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <CoreGraphics/CGColorSpace.h>
@interface DSPDFController : NSObject{
    CGPDFDocumentRef pdf;
    UIImageView *imageView;
    int totalPage;
}
- (instancetype)initPDFWithName:(NSString * )name;
- (UIImage *)getImageFromPDFWithPageIndex:(NSInteger)page andScale:(double)scale;
@end

//
//  DSPDFController.m
//  socketConnect3
//
//  Created by dascomzz on 16/1/27.
//  Copyright © 2016年 DS. All rights reserved.
//

#import "DSPDFController.h"

@implementation DSPDFController
//初始化
- (instancetype)initPDFWithName:(NSString * )name{
    if (self = [super init]) {
        NSString *pdfPath = [[NSBundle mainBundle] pathForResource:name ofType:@"pdf"];
        [self createPDFFromExistFile:pdfPath];
    }
    return self;
}
//创建PDF文件
- (void)createPDFFromExistFile:(NSString *)aFilePath{
    CFStringRef path;
    CFURLRef url;
    path = CFStringCreateWithCString(NULL, [aFilePath UTF8String], kCFStringEncodingUTF8);
    url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, NO);
    CFRelease(path);
    pdf = CGPDFDocumentCreateWithURL(url);
    CFRelease(url);
}
//创建位图上下文
CGContextRef CreateARGBBitmapContext (CGSize size)
{
    CGContextRef context = NULL;
    CGColorSpaceRef colorSpace;
    uint32_t *bitmapData;
    
    size_t bitsPerPixel = 32;
    size_t bitsPerComponent = 8;
    size_t bytesPerPixel = bitsPerPixel / bitsPerComponent;
    
    size_t width = size.width;
    size_t height = size.height;
    
    size_t bytesPerRow = width * bytesPerPixel;
    size_t bufferLength = bytesPerRow * height;
    
    colorSpace = CGColorSpaceCreateDeviceRGB();
    
    if(!colorSpace) {
        NSLog(@"Error allocating color space RGB\n");
        return NULL;
    }
    
    // Allocate memory for image data
    bitmapData = (uint32_t *)malloc(bufferLength);
    
    if(!bitmapData) {
        NSLog(@"Error allocating memory for bitmap\n");
        CGColorSpaceRelease(colorSpace);
        return NULL;
    }
    
    //Create bitmap context
    
    context = CGBitmapContextCreate(bitmapData,
                                    width,
                                    height,
                                    bitsPerComponent,
                                    bytesPerRow,
                                    colorSpace,
                                    kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);	// RGBA
    if(!context) {
        free(bitmapData);
        NSLog(@"Bitmap context not created");
    }
    
    CGColorSpaceRelease(colorSpace);
    
    return context;
}
//PDF转换为位图
- (CGImageRef) PDFPageToCGImage:(size_t)pageNumber inDoc:(CGPDFDocumentRef) document atScale:(float) scale {
    CGPDFPageRef	page;
    CGRect          pageSize;
    CGContextRef	outContext;
    CGImageRef	ThePDFImage;
    
    page = CGPDFDocumentGetPage (document, pageNumber);
    if(page)
    {
        pageSize = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
        CGSize sizeTo = pageSize.size;
        if (scale != 0) {
            sizeTo.width = sizeTo.width * scale;
            sizeTo.height = sizeTo.height * scale;
        }
        else {
            
        }
        
        outContext= CreateARGBBitmapContext (sizeTo);
        if(outContext)
        {
            // Scale the context so that the PDF page is rendered
            // at the correct size for the zoom level.
            CGContextScaleCTM(outContext, scale, scale);
            CGContextDrawPDFPage(outContext, page);
            
            ThePDFImage= CGBitmapContextCreateImage(outContext);
            CGContextRelease(outContext);
            CGPDFPageRelease(page);
            return ThePDFImage;  
        }
    }
    return NULL;
}


//根据页码获得PDF相应的图片
- (UIImage *)getImageFromPDFWithPageIndex:(NSInteger)page andScale:(double)scale{
    if (scale <= 0) {
        scale = 1;
    }
    CGImageRef imageRef = [self PDFPageToCGImage:page inDoc:pdf atScale:scale];
    return [UIImage imageWithCGImage:imageRef];
}
@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值