views:

684

answers:

2

THANKS..This Works now.

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"];
A: 

Hi Michael,

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,

Ben Gotow
I'm getting a error expected ":" before ";" on this line: NSString *path = [[@"~Documents"] pathForResource:@"image" ofType:@"png"]; Any Ideas? Thanks for both you help.
Michael Robinson
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.
Ben Gotow
Working now thanks...
Michael Robinson
A: 
- (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!

Jordan
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? Thanks
Michael Robinson
[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.
Jordan
Working now thanks...
Michael Robinson