Saving image to Documents directory and retrieving for email attachment

血红的双手。 提交于 2019-12-27 16:48:45

问题


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

回答1:


- (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!




回答2:


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.




回答3:


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,




回答4:


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


来源:https://stackoverflow.com/questions/2037432/saving-image-to-documents-directory-and-retrieving-for-email-attachment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!