Freedom4ever
Freedom4ever

Reputation: 93

Saving screenshot to sandbox

Is there any way to save a screenshot to the application's sandbox or some where that can be easily found to add to an email attachment? currently im saving it in the camera roll but can't seem to be able to get it to attach to my email

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

Upvotes: 2

Views: 422

Answers (2)

Daniel
Daniel

Reputation: 23359

You should check out writeToFile:atomically: you get your image data, and write it to a file, this will save to the application's sandbox.

Check out this for some example code: 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];

You can then use the addAttachmentData:mimeType:fileName: method on MFMailComposeViewController class to attach an attachment (actually you don't even need to save the image to disk):

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];

Be sure to have a correct MIME type set here. The file name you can set to what you want, it is the name of the file in the attachment as seen by the recipient.

Upvotes: 1

Jesse Black
Jesse Black

Reputation: 7976

Here are 2 bare bones examples of how to attach an image to an email.

// 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];

To save the image in the documents directory in the app's sandbox

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

I cleaned up the construction of the path, it seems your code has a path like

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

Upvotes: 1

Related Questions