iOS PDF文件链接地址转化成UIImage保存的相册

2020-09-02  本文已影响0人  水晶屋_Crystal

方式一

- (void)saveToAlbumClick{
    NSString *pdfUrlStr = @"http://dzfp.jsjinfo.cn:8080/SSMInvoice/uploads/FPXZ/20200707_011002000111_09946296.pdf";
    NSURL *url = [[NSURL alloc] initWithString:pdfUrlStr];
    CFURLRef ref = (__bridge CFURLRef)url;
    CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(ref);
    CFRelease(ref);
    
    CGImageRef imageRef = [self pdfPageToCGImage:1 documentRef:pdf];
    UIImage *img = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        [OMGToast showWithText:@"保存图片失败!"];
    } else {
        [OMGToast showWithText:@"保存图片成功!"];
    }
}

- (CGImageRef)pdfPageToCGImage:(size_t)pageNumber documentRef:(CGPDFDocumentRef )document {
    CGPDFPageRef page = CGPDFDocumentGetPage(document, pageNumber);
    if (page) {
        CGRect pageSize = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
        CGContextRef outContext = [self createARGBBitmapContext:pageSize.size.width high:pageSize.size.height];
        if (outContext) {
            CGContextDrawPDFPage(outContext, page);
            CGImageRef thePdfImage = CGBitmapContextCreateImage(outContext);
            CFRelease(outContext);
            return thePdfImage;
        }
    }
    return NULL;
}

- (CGContextRef)createARGBBitmapContext:(size_t)pixelsWide high:(size_t)pixelsHigh {
    CGContextRef context = NULL;
    CGColorSpaceRef colorSpace;
    void * bitmapData;
    unsigned long bitmapByteCount;
    unsigned long bitmapBytesPerRow;
    bitmapBytesPerRow = (pixelsWide * 4);
    bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
    colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
    if (colorSpace == NULL) {
        fprintf(stderr, "Error allocating color space\n");
        return NULL;
    }
    bitmapData = malloc(bitmapByteCount);
    if (bitmapData == NULL) {
        fprintf(stderr, "Memory not allocated");
        CGColorSpaceRelease(colorSpace);
        return NULL;
    }
    context = CGBitmapContextCreate(bitmapData, pixelsWide, pixelsHigh, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst);
    if (context == NULL) {
        free(bitmapData);
        fprintf(stderr, "Context not created!");
    }
    CGColorSpaceRelease(colorSpace);
    return context;
}

方式二

- (IBAction)saveToAlumClick:(id)sender {
    NSString *pdfStr = @"http://dzfp.jsjinfo.cn:8080/SSMInvoice/uploads/FPXZ/20200707_011002000111_09946296.pdf";
    NSURL *url = [[NSURL alloc] initWithString:pdfStr];
    CFURLRef ref = (__bridge CFURLRef)url;
    CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(ref);
    CFRelease(ref);
    //pdf文件包括的页数
    size_t pageCount = CGPDFDocumentGetNumberOfPages(pdf);
    for (size_t i = 1; i <= pageCount; i ++) {
        UIImage *img = [self getUIImageFromPDFPage:i documentRef:pdf];
        UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }
}

- (UIImage *)getUIImageFromPDFPage:(size_t)pageNumber documentRef:(CGPDFDocumentRef)document {
    CGPDFPageRef page = CGPDFDocumentGetPage(document, pageNumber);
    CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
    pageRect.origin = CGPointZero;
    pageRect.size.width = pageRect.size.width * 2;
    pageRect.size.height = pageRect.size.height * 2;
    
    //开启图片绘制上下文
    UIGraphicsBeginImageContext(pageRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //设置白色背景
    CGContextSetRGBFillColor(context, 1, 1, 1, 1);
    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();
    return pdfImage;
}

两种方式获取到图片区别:

方式一所得的图片没有方式二所得的图片清晰。

上一篇下一篇

猜你喜欢

热点阅读