views:

166

answers:

1

Hi,

I am new to iphone development. I have created a button in the view. On clicking the button it loads the photolibrary from the Iphone. Now i want to attached the those selected image through mail. I donno how to attach the image in MFMailComposerView.

How can i achieve this,

Here my code is,

-(IBAction) Pictures:(id)sender
{ 
    self.imgpicker = [[UIImagePickerController alloc] init];
    self.imgpicker.delegate = self;
    self.imgpicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentModalViewController:self.imgpicker animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img1    editingInfo:(NSDictionary *)editInfo {
    [[picker parentViewController] dismissModalViewControllerAnimated:NO]; 
    UIView *view = [[UIView alloc] init];   (This view for displaying the images)
    imageview = [[UIImageView alloc] initWithImage:img1];
    [imageview setFrame:CGRectMake(0, 0, 320, 420)];
   [self.view addSubview:imageview];
    [view release];

  UIBarButtonItem *rightbutton = [[UIBarButtonItem alloc] initWithTitle:@"Email"   style:UIBarButtonItemStyleBordered target:self action:@selector(rightbutton)];
  self.navigationItem.rightBarButtonItem = rightbutton;
  [rightbutton release];

   }

  -(void) rightbutton
  {
      [self emailImage:(UIImage *)image];( how to pass the image to mail view)
  }

  - (void)emailImage:(UIImage *)image
  {
      picker = [[MFMailComposeViewController alloc] init];
      picker.mailComposeDelegate = self;
      [picker setToRecipients:[NSArray arrayWithObjects:@"[email protected]",nil]];
      NSData *data = UIImagePNGRepresentation(image);
      [picker addAttachmentData:data mimeType:@"image/png" fileName:@"iPod Library Image"]; 
      [self presentModalViewController:picker animated:YES];
      [picker release];
    }

Please help me out.

Thanks.

+1  A: 

You should probably set up a UIImage instance variable in your controller class (imageThatWasPicked or the like), then set that instance variable when you get the image from -imagePickerController:didFinishPickingImage:editingInfo:. You could then refer to this instance variable when calling -emailImage:.

As a note, you are leaking imageView, and I have no idea what you are trying to do with view in the image picker delegate method. You could probably get rid of the code allocating and releasing view entirely.

Brad Larson