ios 将图像保存到 Documents 目录并检索电子邮件附件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2037432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:46:44  来源:igfitidea点击:

Saving image to Documents directory and retrieving for email attachment

iosiphoneemailnsdocument

提问by Michael Robinson

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

我无法找出NSBundle& DocumentDirectory 数据,我有一个相机图片“ imageView”,我正在保存它NSDocumentDirectoy,然后想要检索它以附加到电子邮件中,

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

采纳答案by Jordan

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

这应该让你开始!

回答by Suragch

Swift 3

斯威夫特 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 guardor if letin your code.

为简洁起见,我强制展开了一些可选选项。在您的代码中使用guardif let

回答by S R Nayak

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

回答by Ben Gotow

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.

因为每个 iPhone 应用程序都在它自己的沙箱中,所以您无权访问设备范围的文档文件夹。要将图像附加到电子邮件,请将图像保存在您自己的文档文件夹中。尝试使用[@"~/Documents" StringByExpandingTildeInPath]来获取您的本地文档文件夹 - 这对我有用。看起来您用于将图像附加到电子邮件的技术是正确的。

Hope that helps,

希望有所帮助,