使用 mailcore 附加照片数组
Attach array of photos with mailcore
我需要使用 MailCore 将一系列照片附加到电子邮件中。我在另一个问题中找到了以下代码,但是我不确定如何将其应用到我的应用程序中用相机拍摄的照片。
我的猜测是我必须获取照片的文件名并将它们作为字符串保存到数组中,然后使用代码片段将字符串附加到电子邮件中。
NSArray *allAttachments = [NSArray arrayWithObjects:@{@"FilePathOnDevice": @"/var/mobile/etc..", @"FileTitle": @"IMG_0522.JPG"}, nil];
for (int x = 0; x < allAttachments.count; x++) {
NSString *attachmentPath = [[allAttachments objectAtIndex:x] valueForKey:@"FilePathOnDevice"]];
MCOAttachment *attachment = [MCOAttachment attachmentWithContentsOfFile:attachmentPath];
[msgBuilder addAttachment:attachment];
}
这就是我使用相机拍摄照片的方式
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){
imagePicker.sourceType = .Camera
imagePicker.dismissViewControllerAnimated(true, completion: nil)
var photo = info[UIImagePickerControllerOriginalImage] as? UIImage
bedroomCells[lastSelectedIndex!.row].image = photo
tableView.reloadData()
}
感谢您的帮助!
我最终将图像保存到文档目录并使用该文件路径将图像附加到 mailcore。
这是我的图像选择器代码:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){
imagePicker.sourceType = .Camera
imagePicker.dismissViewControllerAnimated(true, completion: nil)
var photo = info[UIImagePickerControllerOriginalImage] as? UIImage
bedroomCells[lastSelectedIndex!.row].image = photo
var photoLabel = bedroomCells[lastSelectedIndex!.row].text
tableView.reloadData()
//save photo to document directory
var imageData = UIImageJPEGRepresentation(photo, 1.0)
var imageFilePath = fileDirectory[0].stringByAppendingPathComponent("\(photoLabel!).jpg")
var imageFileURL = NSURL(fileURLWithPath: imageFilePath)
imageData.writeToURL(imageFileURL!, atomically: false)
}
以及我的 mailcore 方法的代码:
MCOMessageBuilder * builder = [[MCOMessageBuilder alloc] init];
NSString *documentsDirectory = [fileDirectory objectAtIndex:0];
NSArray *allAttachments = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
for (int x = 0; x < allAttachments.count; x++) {
NSString *attachmentPath = [documentsDirectory stringByAppendingPathComponent:[allAttachments objectAtIndex:x]];
MCOAttachment *attachment = [MCOAttachment attachmentWithContentsOfFile:attachmentPath];
[builder addAttachment:attachment];
}
在swift代码中:
var dataImage: NSData?
dataImage = UIImageJPEGRepresentation(image, 0.6)!
var attachment = MCOAttachment()
attachment.mimeType = "image/jpg"
attachment.filename = "image.jpg"
attachment.data = dataImage
builder.addAttachment(attachment)
我需要使用 MailCore 将一系列照片附加到电子邮件中。我在另一个问题中找到了以下代码,但是我不确定如何将其应用到我的应用程序中用相机拍摄的照片。
我的猜测是我必须获取照片的文件名并将它们作为字符串保存到数组中,然后使用代码片段将字符串附加到电子邮件中。
NSArray *allAttachments = [NSArray arrayWithObjects:@{@"FilePathOnDevice": @"/var/mobile/etc..", @"FileTitle": @"IMG_0522.JPG"}, nil];
for (int x = 0; x < allAttachments.count; x++) {
NSString *attachmentPath = [[allAttachments objectAtIndex:x] valueForKey:@"FilePathOnDevice"]];
MCOAttachment *attachment = [MCOAttachment attachmentWithContentsOfFile:attachmentPath];
[msgBuilder addAttachment:attachment];
}
这就是我使用相机拍摄照片的方式
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){
imagePicker.sourceType = .Camera
imagePicker.dismissViewControllerAnimated(true, completion: nil)
var photo = info[UIImagePickerControllerOriginalImage] as? UIImage
bedroomCells[lastSelectedIndex!.row].image = photo
tableView.reloadData()
}
感谢您的帮助!
我最终将图像保存到文档目录并使用该文件路径将图像附加到 mailcore。
这是我的图像选择器代码:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){
imagePicker.sourceType = .Camera
imagePicker.dismissViewControllerAnimated(true, completion: nil)
var photo = info[UIImagePickerControllerOriginalImage] as? UIImage
bedroomCells[lastSelectedIndex!.row].image = photo
var photoLabel = bedroomCells[lastSelectedIndex!.row].text
tableView.reloadData()
//save photo to document directory
var imageData = UIImageJPEGRepresentation(photo, 1.0)
var imageFilePath = fileDirectory[0].stringByAppendingPathComponent("\(photoLabel!).jpg")
var imageFileURL = NSURL(fileURLWithPath: imageFilePath)
imageData.writeToURL(imageFileURL!, atomically: false)
}
以及我的 mailcore 方法的代码:
MCOMessageBuilder * builder = [[MCOMessageBuilder alloc] init];
NSString *documentsDirectory = [fileDirectory objectAtIndex:0];
NSArray *allAttachments = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
for (int x = 0; x < allAttachments.count; x++) {
NSString *attachmentPath = [documentsDirectory stringByAppendingPathComponent:[allAttachments objectAtIndex:x]];
MCOAttachment *attachment = [MCOAttachment attachmentWithContentsOfFile:attachmentPath];
[builder addAttachment:attachment];
}
在swift代码中:
var dataImage: NSData?
dataImage = UIImageJPEGRepresentation(image, 0.6)!
var attachment = MCOAttachment()
attachment.mimeType = "image/jpg"
attachment.filename = "image.jpg"
attachment.data = dataImage
builder.addAttachment(attachment)