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

发布于 2024-08-17 17:43:53 字数 1146 浏览 10 评论 0原文

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

这里是保存代码:

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

这是新的获取数据代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

抹茶夏天i‖ 2024-08-24 17:43:53
- (IBAction)getImage {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
    UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
}

这应该可以帮助您入门!

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

好菇凉咱不稀罕他 2024-08-24 17:43:53

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
// ...

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

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.

近箐 2024-08-24 17:43:53

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

希望有帮助,

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,

平定天下 2024-08-24 17:43:53

试试这个:

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

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文