将屏幕截图保存到沙箱

时间:2012-08-14 02:11:01

标签: ios xcode screenshot sandbox

有没有办法将屏幕截图保存到应用程序的沙箱或某些可以轻松找到添加到电子邮件附件的地方?目前我将其保存在相机胶卷中,但似乎无法将其附加到我的电子邮件

NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
NSString *png = @".png";
NSString *filename = [drawquestion stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *link = [NSString stringWithFormat: @"%@%@%@", documentDirectory,filename,png];
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:link];
NSLog(link);

UIImageWriteToSavedPhotosAlbum(viewImage, drawquestion, nil, nil);

2 个答案:

答案 0 :(得分:1)

您应该查看writeToFile:atomically:获取图像数据,并将其写入文件,这将保存到应用程序的沙箱中。

查看此示例代码:http://blog.objectgraph.com/index.php/2010/04/05/download-an-image-and-save-it-as-png-or-jpeg-in-iphone-sdk/

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSLog(@"%@",docDir);

NSLog(@"saving png");
NSString *pngFilePath = [NSString stringWithFormat:@"%@/test.png",docDir];

    // You should swap data for your image data
NSData *data = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data writeToFile:pngFilePath atomically:YES];

然后,您可以使用addAttachmentData:mimeType:fileName:类上的MFMailComposeViewController方法附加附件(实际上您甚至不需要将图像保存到磁盘):

MFMailComposeViewController * mailVC = [[MFMailComposeViewController alloc] init];

NSData *data; // this is the data from earlier
[mailVC addAttachmentData:data mimeType:@"image/png" fileName:@"myfilename"];
[self presentModalViewController:mailVC animated:YES];

请务必在此处设置正确的MIME类型。您可以将文件名设置为您想要的文件名,它是收件人看到的附件中文件的名称。

答案 1 :(得分:1)

以下是有关如何将图像附加到电子邮件的2个简单示例。

// with UIImage * image;

  MFMailComposeViewController * mfcvc = [[[MFMailComposeViewController alloc] init] autorelease];

  NSData * imageData = UIImagePNGRepresentation(image);
  [mfcvc addAttachmentData:imageData mimeType:@"image/png" fileName:@"demo"];
  [self.viewController presentModalViewController:mfcvc animated:YES];

// with UIImage * image; and float compression_quality; between 0.0 and 1.0

  MFMailComposeViewController * mfcvc = [[[MFMailComposeViewController alloc] init] autorelease];

  NSData * imageData = UIImageJPEGRepresentation(image, compression_quality);
  [mfcvc addAttachmentData:imageData mimeType:@"image/jpeg" fileName:@"demo"];
  [self.viewController presentModalViewController:mfcvc animated:YES];

将图像保存在应用程序沙箱的文档目录中

NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
NSString *png = @".png";
NSString *filename = [drawquestion stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *imagePath = [documentDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@",filename, png]];

[imageData writeToFile:imagePath atomically:YES];

PS

我清理了路径的构造,似乎你的代码有一个像

这样的路径
  

/var/mobile/Applications/APP_ID//var/mobile/Applications/APP_ID/Documents/filename.png