首页 技术 正文
技术 2022年11月15日
0 收藏 917 点赞 2,405 浏览 2101 个字

1.无论:TXT,EPUB,PDF等各种格式的文件,保存到本地的时候,最好都保存成字母或者数字,不要保存成汉字,否则,在取文件的时候,由于编码的问题,各种瓦特

2.如果文件名真的保存成了汉字,那么进行转码的方法是:

[fileName
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];//不这样处理会返回nil];

3.pdf的坑:由于后台返回的文件格式都是dat,需要本地动态判断文件格式,因此在判断PDF的时候,就遇到了很多问题

1. 在NSURL和filePath之间转换的时候,虽然filePath是一个string,但是不能用URL.absoluteString这个方法,应该用URL.path 这个方法,不然后面取不到文件

string转URL的时候,这样:

[NSURL URLWithString:_model.filePath]

2,把文件路径中的.dat 替换成 .pdf,没卵用:

[aFilePath  stringByReplacingOccurrencesOfString:@".dat" withString:@".pdf"]

必须重命名文件名

//获取本地的PDF文件
+(CGPDFDocumentRef)pdfRefByFilePath:(NSString *)aFilePath
{
// CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), (__bridge CFStringRef)[aFilePath lastPathComponent], NULL, (__bridge CFStringRef)@"files");
// CGPDFDocumentRef pdfDocument = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
//
CFStringRef path;
CFURLRef url;
CGPDFDocumentRef document;
size_t count; //pdf的扩展名必须重命名一下,才可以取到
NSString* aFilePath2 = [aFilePath stringByReplacingOccurrencesOfString:@".dat" withString:@".pdf"];
NSError* error;
//[[NSFileManager defaultManager] moveItemAtPath:aFilePath2 toPath:aFilePath2 error:nil];
NSFileManager* fileManager =[NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:aFilePath]) {
//get new resource path with different extension //copy it over
[fileManager copyItemAtPath:aFilePath toPath:aFilePath2 error:&error];
} path = CFStringCreateWithCString (NULL, [aFilePath2 UTF8String],
kCFStringEncodingUTF8);
url = CFURLCreateWithFileSystemPath (NULL, path,
kCFURLPOSIXPathStyle, );
CFRelease (path);
document = CGPDFDocumentCreateWithURL (url);
CFRelease(url);
if (document == nil) {
[fileManager copyItemAtPath:aFilePath2 toPath:aFilePath error:&error];
}// NSString* filePath = [aFilePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// CFStringRef path;
// CFURLRef url;
// CGPDFDocumentRef document;
// filePath = [aFilePath stringByReplacingOccurrencesOfString:@".dat" withString:@".pdf"];
// path = CFStringCreateWithCString(NULL, [filePath UTF8String], kCFStringEncodingUTF8);
// url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
// CFRelease(path);
// document = CGPDFDocumentCreateWithURL(url);
// CFRelease(url); return document;}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905