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

2020/11/25 01:11 · ios ·  · 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"];

- (IBAction)getImage {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
    UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
}

这应该让您开始!

迅捷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

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

希望能有所帮助,

试试这个:

 -(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];
}
本文地址:http://ios.askforanswer.com/jiangtuxiangbaocundaowendangmulubingjiansuodianziyoujianfujian.html
文章标签: ,   ,   ,  
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!

文件下载

老薛主机终身7折优惠码boke112

上一篇:
下一篇:

评论已关闭!