Saving image to Documents directory and retrieving for email attachment
Asked Answered
P

4

72

I having trouble figuring out NSBundle & DocumentDirectory data, I have a Camera Picture "imageView" that I'm saving to the NSDocumentDirectoy and then want to retrieve it for attaching to an email,

Here the saving 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 getting data 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"];
Pixie answered 10/1, 2010 at 15:24 Comment(2)
can you tell me how you got the camera image in the first place please? i.e. the reference to the UIImage that you took with the camera. Thanks.Forest
where to call saveImage action. I am new on the Xcode.Baltazar
A
66
- (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!

Await answered 10/1, 2010 at 16:14 Comment(4)
Thanks, It's different than Ben's answer but the email attachment needs NSData to be added, I must be missing something in you answer. How would I attach the "img: named to the email? ThanksPixie
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"icon"]; The "data" above is the nsdata representation of the image. "picker" is the email component. it's already attached.Await
Does saving it to documents automatically generate a unique file name for the image?Jerkin
When you save to documents, you pass in a file path yourself. You can create a temp file using: #216320Await
E
5

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 force unwrapped some of the optionals for brevity. Use guard or if let in your code.

Ecotype answered 6/2, 2016 at 11:46 Comment(2)
In fact there are two available cases - .documentationDirectory and .documentDirectory, which are not the same thing. The question was about documents directory so I guess you just picked a wrong value from code completion suggestions ;)Aeolis
@alexburtnik, ah, yes, probably. Thank your for your explanation (and for fixing my mistake).Ecotype
P
0

Because each iPhone app is in it's own sandbox, you don't have access to a device-wide documents folder. To attach an image to an email, save the image in your own documents folder. Try using [@"~/Documents" StringByExpandingTildeInPath] to get your local documents folder - that works for me. It looks like the technique you're using for attaching the image to an email is correct.

Hope that helps,

Pericarditis answered 10/1, 2010 at 15:34 Comment(2)
I'm getting a error expected ":" before ";" on this line: NSString *path = [[@"~Documents"] pathForResource:@"image" ofType:@"png"]; Any Ideas? Thanks for both you help.Pixie
Remember to add the "stringByExpandingTildeInPath" part. (And it needs to be "~/Documents", not "~Documents". stringByExpandingTildeInPath is a method of NSString that will automatically replace the ~ with the path to the user's home folder.Pericarditis
C
0

Try this one :

 -(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];
}
Chaparro answered 5/6, 2014 at 6:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.