Saving image to Documents directory and retrieving for email attachment

前端 未结 4 1438
遇见更好的自我
遇见更好的自我 2020-11-28 04:07

I having trouble figuring out NSBundle & DocumentDirectory data, I have a Camera Picture \"imageView\" that I\'m saving to the NSDocumentDire

相关标签:
4条回答
  • 2020-11-28 04:22

    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];
    }
    
    0 讨论(0)
  • 2020-11-28 04:27

    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,

    0 讨论(0)
  • 2020-11-28 04:34

    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.

    0 讨论(0)
  • 2020-11-28 04:44
    - (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!

    0 讨论(0)
提交回复
热议问题