iOS圖片轉(zhuǎn)PDF+導(dǎo)出

流程:設(shè)置PDF保存路徑->將圖片轉(zhuǎn)成PDF并存放到設(shè)置好的路徑->將生成好的PDF轉(zhuǎn)Data使用原生分享功能分享出去。

1、設(shè)置PDF保存路徑


#pragma mark - 創(chuàng)建PDF儲(chǔ)存路徑

- (NSString *)createPDFPathWithName:(NSString *)pdfName {
    NSFileManager * fileManager = [NSFileManager defaultManager];

    NSString * finderPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
                                                                  NSUserDomainMask, YES) lastObject]
                             stringByAppendingPathComponent:@"PDF"];
    
    if (![fileManager fileExistsAtPath:finderPath])
    {
        [fileManager createDirectoryAtPath:finderPath withIntermediateDirectories:YES
                                attributes:nil
                                     error:NULL];
    }
    return [finderPath stringByAppendingPathComponent:pdfName];
}

2、生成PDF


#pragma mark - 創(chuàng)建PDF

- (NSString *)createPDF {
    NSString * pdfPath = [self createPDFPathWithName:@"test.pdf"];
 
    // CGRectZero 表示默認(rèn)尺寸,參數(shù)可修改,設(shè)置自己需要的尺寸
    UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, NULL);
    
    CGRect  pdfBounds = UIGraphicsGetPDFContextBounds();
    CGFloat pdfWidth  = pdfBounds.size.width;
    CGFloat pdfHeight = pdfBounds.size.height;
    
    [self.images enumerateObjectsUsingBlock:^(UIImage * _Nonnull image, NSUInteger idx, BOOL * _Nonnull stop) {
        // 繪制PDF
        UIGraphicsBeginPDFPage();
        
        CGFloat imageW = image.size.width;
        CGFloat imageH = image.size.height;
        
        if (imageW <= pdfWidth && imageH <= pdfHeight)
        {
            CGFloat originX = (pdfWidth - imageW) / 2;
            CGFloat originY = (pdfHeight - imageH) / 2;
            [image drawInRect:CGRectMake(originX, originY, imageW, imageH)];
        }
        else
        {
            CGFloat width,height;

            if ((imageW / imageH) > (pdfWidth / pdfHeight))
            {
                width  = pdfWidth;
                height = width * imageH / imageW;
            }
            else
            {
                height = pdfHeight;
                width = height * imageW / imageH;
            }
            [image drawInRect:CGRectMake((pdfWidth - width) / 2, (pdfHeight - height) / 2, width, height)];
        }
    }];
    
    UIGraphicsEndPDFContext();

    
    return pdfPath;
}

3、分享


#pragma mark - 分享

- (void)shareClick {
    NSString * path = [self createPDF];
    
    NSURL *  file = [NSURL fileURLWithPath:path];
    NSData * data = [NSData dataWithContentsOfFile:path];
    
    UIActivityViewController * activity = [[UIActivityViewController alloc]initWithActivityItems:@[data,file]
                                                                           applicationActivities:nil];
    [self presentViewController:activity animated:YES completion:nil];
    
}

4、demo地址:demo

5、效果圖

圖一

?


圖二
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容