我想将NSMutableArray的内容转换为NSData,然后将其转换为pdf。 我使用以下代码来转换NSdata,但它给出了错误。我搜索了很多文章,但没有得到任何东西
myArray=[[NSMutableArray alloc]init];
[myArray addObject:@"Jamshed"];
[myArray addObject:@"Imran"];
[myArray addObject:@"Ali"];
[myArray addObject:@"Hussain"];
[myArray addObject:@"Faisal"];
for (int i=0; i<[myArray count]; i++)
{
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:[myArray objectAtIndex:i]];
NSLog(@"data %@",data);
//create code for pdf file for write b4 read and concatenate readed string with data to write in pdf file.
}
- (NSData*) pdfDataWithSomeText;
{
// For more on generating PDFs, see http://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratingPDF/GeneratingPDF.html
// The PDF content will be accumulated into this data object.
NSMutableData *pdfData = [NSMutableData data];
// Use the system default font.
UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
// Use the default page size of 612*792.
CGRect pageRect = CGRectMake(0, 0, 612, 792);
// Use the defaults for the document, and no metadata.
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);
// Create a page.
UIGraphicsBeginPDFPageWithInfo(pageRect, nil);
// Store some placeholder text.
NSString *topLine = @"PDF Sample from";
NSString *bottomLine = @"http://stackoverflow.com/q/10122216/1318452";
// Draw that placeholder text, starting from the top left.
CGPoint topLeft = CGPointZero;
CGSize lineSize = [topLine sizeWithFont:font];
[topLine drawAtPoint:topLeft withFont:font];
// Move down by the size of the first line before drawing the second.
topLeft.y += lineSize.height;
[bottomLine drawAtPoint:topLeft withFont:font];
// Close the PDF context.
UIGraphicsEndPDFContext();
// The pdfData object has now had a complete PDF file written to it.
return pdfData;
}
答案 0 :(得分:3)
将字符串写入PDF并不像从这些字符串生成NSData那么简单。看看Drawing and Printing Guide for iOS - Generating PDF Content。是的,这是一份大文件。阅读。试试它的例子。尝试将自己的字符串添加到他们的示例中然后,如果你的东西几乎有效,请回到这里询问一下。
所以这里是上面链接的代码,通过使用NSString而不是Core Text绘制更简单。它绘制输入数组,但可能需要一些更好的算法。你能用更有条理的方式画画吗?
- (NSData*) pdfDataWithStrings:(NSArray*) strings;
{
// For more on generating PDFs, see http://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratingPDF/GeneratingPDF.html
strings = [strings arrayByAddingObject:@"https://stackoverflow.com/q/10122216/1318452"];
// The PDF content will be accumulated into this data object.
NSMutableData *pdfData = [NSMutableData data];
// Use the system default font.
UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
// Use the default page size of 612*792.
CGRect pageRect = CGRectMake(0, 0, 612, 792);
// Use the defaults for the document, and no metadata.
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);
// Create a page.
UIGraphicsBeginPDFPageWithInfo(pageRect, nil);
// Add the strings within the space of the pageRect.
// If you want to draw the strings in a column or row, in order, you will need to change this bit.
for (NSString *line in strings)
{
// Hint: you will still need to know the lineSize.
CGSize lineSize = [line sizeWithFont:font];
CGFloat x = pageRect.origin.x + (arc4random_uniform(RAND_MAX)/(CGFloat) RAND_MAX*(pageRect.size.width-lineSize.width));
CGFloat y = pageRect.origin.y + (arc4random_uniform(RAND_MAX)/(CGFloat) RAND_MAX*(pageRect.size.height-lineSize.height));
CGPoint lineTopLeft = CGPointMake(x, y);
// Having worked out coordinates, draw the line.
[line drawAtPoint:lineTopLeft withFont:font];
}
// Close the PDF context.
UIGraphicsEndPDFContext();
// The pdfData object has now had a complete PDF file written to it.
return pdfData;
}
要保存文档,您需要找到保存用户文档的路径:
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
您将在该路径中保存文件。对于最终应用,请让用户选择自己的名称。对于此示例,名称将构建到程序中。
NSString *pdfFilename = @"StackOverflow.pdf";
NSString有一些很好的路径操作方法,可以很容易地构建你要编写的路径。
NSString *pdfPath = [documentsPath stringByAppendingPathComponent:pdfFilename];
然后获取您将要写入该路径的数据。在这里,我将调用上面声明的方法。
NSData *pdfData = [self pdfDataWithStrings:myArray];
将这些数据写入路径。对于更好的应用程序,您可能在某个时候想要调用[pdfData writeToFile:options:error:],这样您就可以显示出错的任何内容。
[pdfData writeToFile:pdfPath atomically:NO];
你怎么知道这是否有效?在模拟器上,您可以记录您写入的路径。在Finder中打开此路径,看看它是否包含您期望的PDF。
NSLog(@"Wrote PDF to %@", pdfPath);
在实际设备上,您可以启用iTunes文件共享。见How to enable file sharing for my app?
答案 1 :(得分:0)
在做之前
NSData *data = [NSKeyedArchiver archivedDataWithRootObject: myArray];
首先将数组转换为json字符串
NSString *jsonString = [myArray JSONRepresentation];
您必须先导入json.h api
答案 2 :(得分:0)
您可以使用
转换为json NSData *jsonData = [NSJSONSerialization dataWithJSONObject:finalDict options:NSJSONWritingPrettyPrinted error:nil];
答案 3 :(得分:-1)
myArray=[[NSMutableArray alloc]init];
[myArray addObject:@"Jamshed"];
[myArray addObject:@"Imran"];
[myArray addObject:@"Ali"];
[myArray addObject:@"Hussain"];
[myArray addObject:@"Faisal"];
for (int i=0; i<[myArray count]; i++)
{
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:[myArray objectAtIndex:i]];
NSLog(@"data %@",data);
//create code for pdf file for write b4 read and concatenate readed string with data to write in pdf file.
}