将图像保存到文档目录并检索电子邮件附件
我无法弄清楚 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这应该可以帮助您入门!
This should get you started!
Swift 3
为了简洁起见,我强制解开一些选项。在代码中使用
guard
或if let
。Swift 3
I force unwrapped some of the optionals for brevity. Use
guard
orif let
in your code.由于每个 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,
试试这个:
Try this one :