如何将相机胶卷图像附加到邮件编辑器

时间:2013-09-29 13:45:33

标签: ios uiimageview uiimage uiimagepickercontroller

我有一个问题,我将文本和图像附加到邮件编辑器上,我的文本字段附加正常,但是当我尝试附加从我的相机胶卷中选择的图像时,我得到一个问号?是因为我没有从ios设备拍照,我只是从谷歌下载图像来测试它。该应用程序的工作原理如下: 用户拍照并在UIImageView中查看,然后可以输入他的联系方式和姓名并发送,或者可以从他们的相机胶卷中选择一张照片。

·H

@interface xyzViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationBarDelegate> {
IBOutlet UILabel *label;
IBOutlet UITextField *field;
}

@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) IBOutlet UIButton *TakePhoto;
@property (strong, nonatomic) IBOutlet UIButton *SelectPhoto;

- (IBAction)savedata:(id)sender;
- (IBAction)loaddata:(id)sender;

- (IBAction)dismiss:(id)sender;
- (IBAction)dismiss1:(id)sender;


@property (weak, nonatomic) IBOutlet UITextField *mytextview;
@property (weak, nonatomic) IBOutlet UITextField *mytextview1;
@property (weak, nonatomic) IBOutlet UIImageView *image;

@end

.m文件

@interface xyzViewController ()

@end

@implementation xyzViewController


- (IBAction)savedata:(id)sender; {
NSString *savestring = _mytextview.text;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:savestring forKey:@"savedstring"];
[defaults synchronize];

NSString *savestring1 = _mytextview1.text;
NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
[defaults1 setObject:savestring1 forKey:@"savedstring1"];
[defaults synchronize];

}

- (IBAction)loaddata:(id)sender; {

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *loadstring = [defaults objectForKey:@"savedstring"];
[_mytextview setText:loadstring];
[label setText:loadstring];

NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
NSString *loadstring1 = [defaults objectForKey:@"savedstring1"];
[_mytextview1 setText:loadstring1];

}

- (IBAction)dismiss:(id)sender {
[sender resignFirstResponder];
}
- (IBAction)dismiss1:(id)sender {
[sender resignFirstResponder];
}


 - (IBAction)takePhoto:(UIButton *)sender {

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;

[self presentViewController:picker animated:YES completion:NULL];

}

- (IBAction)selectPhoto:(UIButton *)sender {

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

[self presentViewController:picker animated:YES completion:NULL];


}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;

[picker dismissViewControllerAnimated:YES completion:NULL];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

[picker dismissViewControllerAnimated:YES completion:NULL];

}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)sendButton:(id)sender {
MFMailComposeViewController *mailContoller = [[MFMailComposeViewController alloc]init];
[mailContoller setMailComposeDelegate:self];
NSString *email = @"avip606@gmail.com";
NSString *email1 = @"avi_sp@hotmail.co.uk";
NSArray *emailArray = [[NSArray alloc]initWithObjects:email, email1, nil];
NSString *message = [@[_mytextview.text, _mytextview1.text] componentsJoinedByString: @"\n"];
[mailContoller setMessageBody:message isHTML:NO];
NSData *data = UIImagePNGRepresentation(_image);
[mailContoller addAttachmentData:data
                        mimeType:@"image/png"
                        fileName:@"image.png"];
[mailContoller setToRecipients:emailArray];
[mailContoller setSubject:@"IT WORKS!"];
[self presentViewController:mailContoller animated:YES completion:nil];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[[self mytextview] resignFirstResponder];
}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:nil];
}


@end

1 个答案:

答案 0 :(得分:1)

拍摄照片

- (IBAction)takePhotoCam:(id)sender {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    [self.navigationController presentViewController:imagePicker animated:YES completion:nil];
}

从图库使用中选择

- (IBAction)choosePhotoFromLibrary:(id)sender {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    [self.navigationController presentViewController:imagePicker animated:YES completion:nil];
}

现在使用委托阅读该照片并附在电子邮件中......

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    NSData *pngData = UIImagePNGRepresentation(image);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
    filePath = [documentsPath stringByAppendingPathComponent:@"myCameraImage.png"];

    NSLog(@"saving data at === %@", filePath);
    [pngData writeToFile:filePath atomically:YES]; //Write the file

    [self.navigationController dismissViewControllerAnimated:YES completion:nil];

}

现在在Documents文件夹中,您有一个名为myCameraImage.png

的图像

现在假设您有一个名为Send on email的按钮。点击创建IBAction并将myCameraImage.png附加到电子邮件。

请参阅here,了解如何将图片附加到电子邮件中。