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"];
- (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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With