2

有什么方法可以将屏幕截图保存到应用程序的沙箱中,或者可以很容易地找到添加到电子邮件附件的地方?目前我将其保存在相机胶卷中,但似乎无法将其附加到我的电子邮件中

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);
4

2 回答 2

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 类型。您可以设置为您想要的文件名,它是收件人看到的附件中文件的名称。

于 2012-08-14T02:26:39.270 回答
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];

附言

我清理了路径的构建,看来你的代码有一个类似的路径

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

于 2012-08-14T02:34:01.817 回答