Saving an image in the Documents folder and extracting for email attachments

I'm having trouble figuring out the NSBundle and DocumentDirectory data, I have a β€œImage” of the β€œCamera” image, which I save in NSDocumentDirectoy , and then want to get it for attaching to email,

Here's the saved code:

 - (IBAction)saveImage { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"]; UIImage *image = imageView.image; // imageView is my image from camera NSData *imageData = UIImagePNGRepresentation(image); [imageData writeToFile:savedImagePath atomically:NO]; } 

Here is the new data retrieval code:

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"]; NSData *myData = [[[NSData alloc] initWithContentsOfFile:appFile] autorelease]; [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"savedImage"]; 
+71
ios email iphone nsdocument
Jan 10
source share
4 answers
 - (IBAction)getImage { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"]; UIImage *img = [UIImage imageWithContentsOfFile:getImagePath]; } 

This should get you started!

+66
Jan 10
source share

Swift 3

 // Create a URL let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first let imageURL = documentsURL?.appendingPathComponent("MyImageName.png") // save image to URL let myImage = imageView.image! // or wherever you have your UIImage do { try UIImagePNGRepresentation(myImage)?.write(to: imageURL!) } catch {} // Use the URL to retrieve the image for sharing to email, social media, etc. // docController.URL = imageURL // ... 

I have forcibly deployed some of the options for brevity. Use guard or if let in your code.

+5
Feb 06 '16 at 11:46
source share

Since each iPhone application has its own sandbox, you do not have access to the document folder on all devices. To attach the image to email, save the image in the folder of your own documents. Try using [@ "~ / Documents" StringByExpandingTildeInPath] to get the local documents folder - this works for me. It seems that the method you use to attach the image to the email is correct.

Hope this helps,

0
Jan 10
source share

Try the following:

  -(void)setProfilePic { NSArray *docpaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [docpaths objectAtIndex:0]; NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"Image.png"]; NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:imagePath]]; UIImage *thumbNail = [[UIImage alloc] initWithData:imgData]; [profilePic_btn setBackgroundImage:thumbNail forState:UIControlStateNormal]; } 
0
Jun 05 '14 at 6:23
source share



All Articles