【问题标题】:How to conver the data of NSMutableArray to NSData如何将 NSMutableArray 的数据转换为 NSData
【发布时间】:2012-04-12 10:59:29
【问题描述】:

我想将 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;
 }

【问题讨论】:

  • 可能duplicate
  • myArray 是什么类?是NSArray 还是NSMutableArray
  • 您可以编辑以添加您收到的错误消息吗?
  • [NSConcreteMutableData getCharacters:range:]:无法识别的选择器发送到实例 0x68b8340
  • 你想做的没有意义。

标签: iphone objective-c


【解决方案1】:

将字符串写入 PDF 并不像从这些字符串生成 NSData 那样简单。看看Drawing and Printing Guide for iOS - Generating PDF Content。是的,这是一个很大的文件。阅读。尝试其中的示例。尝试将您自己的字符串添加到他们的示例中。然后,如果您有一些几乎可以使用的东西,请回到这里询问。

生成 PDF

所以这里是上面链接中的代码,通过使用 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?

【讨论】:

  • 感谢分享,我将把这个方法称为 pdfDataWithSomeText:NSData
  • 不,你想要的NSData对象是通过这个方法返回的。您需要将一个对象传递给此方法,但输入的类型应为 NSStringNSArray
  • 你说你想要一个 PDF 的 NSData。此方法返回 PDF 的 NSData。你想要你的PDF在哪里?是否要将 PDF 保存到用户的文档目录?要显示 PDF 吗?您要打印 PDF 吗?是否要将 PDF 上传到服务器?
  • 我想保存在 iPhone 应用的 Documents 文件夹中
  • 没关系,但可以在上面的代码下面写下这段代码,以及如何调用该方法
【解决方案2】:

你可以转换成json使用

 NSData *jsonData        =   [NSJSONSerialization dataWithJSONObject:finalDict options:NSJSONWritingPrettyPrinted error:nil];

【讨论】:

    【解决方案3】:

    在做之前

    NSData *data = [NSKeyedArchiver archivedDataWithRootObject: myArray];
    

    你先把数组转换成json字符串

    NSString *jsonString = [myArray JSONRepresentation];
    

    必须先导入 json.h api

    【讨论】:

    • 我不会再找你了,你已经给了 JSON 类链接
    • 您可以下载json api并将其拖入您的应用程序,然后#import json.h并使用代码
    【解决方案4】:
    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.  
    }
    

    【讨论】:

    • 感谢 RKK 可以帮助我如何将这些数据转换为 pdf 并保存
    • 抱歉,这行不通。它只是为每个字符串创建单独的 data 对象,而不是为数组创建一个 data 对象。
    • 没问题。每次您阅读pdf文件并连接每个字符串然后写入它。最后所有字符串都添加到pdf中。
    • @RKK 怎么把这些数据写成 pdf 你能帮我吗
    • 嘿,谁在投票。我的代码有什么问题。详细说明我。我的代码有什么问题。
    猜你喜欢
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多