- (id) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index
{
if ([self.fileURL.lastPathComponent rangeOfString:@".txt"].location != NSNotFound) {
//处理txt格式内容显示有乱码的情况
NSData *fileData = [NSData dataWithContentsOfURL:self.fileURL];
//判断是UNICODE编码
NSString *isUNICODE = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
//还是ANSI编码(-2147483623,-2147482591,-2147482062,-2147481296)encoding 任选一个就可以了
NSString *isANSI = [[NSString alloc] initWithData:fileData encoding:-2147483623];
if (isUNICODE) {
NSString *retStr = [[NSString alloc]initWithCString:[isUNICODE UTF8String] encoding:NSUTF8StringEncoding];
NSData *data = [retStr dataUsingEncoding:NSUTF16StringEncoding];
[data writeToURL:self.fileURL atomically:YES];
}else if(isANSI){
NSData *data = [isANSI dataUsingEncoding:NSUTF16StringEncoding];
[data writeToURL:self.fileURL atomically:YES];
}
}
return self.fileURL;
}
网友评论