Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

MFMailComposer Picture Attachment From Camera

tobyaherberttobyaherbert Posts: 19Registered Users
edited February 2011 in iPhone SDK Development
Hello Everyone!
I am trying to email a photograph that i took with the camera or selected from the camera library. Does anyone know how to do this?

Here is my code:

.h
UIImageView * theimageView;
	UIButton * choosePhoto;
	UIButton * takePhoto;
}

@property (nonatomic, retain) IBOutlet UIImageView * theimageView;
@property (nonatomic, retain) IBOutlet UIButton * choosePhoto;
@property (nonatomic, retain) IBOutlet UIButton * takePhoto;

-(IBAction) getPhoto:(id) sender;

.m
-(IBAction) getPhoto:(id) sender {
	UIImagePickerController * picker = [[UIImagePickerController alloc] init];
	picker.delegate = self;
	
	if((UIButton *) sender == choosePhoto) {
		picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
	} else {
		picker.sourceType = UIImagePickerControllerSourceTypeCamera;
	}
	
	[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
	[picker dismissModalViewControllerAnimated:YES];
	theimageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
	
	NSLog(@"Creating Mail Data!!!");
	NSData *photoData = UIImageJPEGRepresentation([UIImage imageNamed:@"qwerty.png"], 1);
	MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
	[mailPicker addAttachmentData:photoData mimeType:@"image/jpg" fileName:[UIImage imageNamed:theimageView.image]];
	[mailPicker setSubject:@"Chicken Photograph!"];
	[mailPicker setToRecipients:[NSArray arrayWithObject:@"tobyherbert@syn-apps.co.uk"]];
	[mailPicker setDelegate:self];
	[self presentModalViewController:mailPicker animated:YES];
}

i can't seem to get it to accept the image view's image or the objectFoKey.
Thanks in advance
Toby......
Post edited by tobyaherbert on

Replies

  • iSDKiSDK Posts: 1,353Tutorial Authors, Registered Users
    edited February 2011
    I think the better thing to do is set the imageview's image from the method, dismiss the imagepicker, and then perform a void method after 0.5 seconds of the pickerview dismissing which would present the mailcomposer.
    - (void)imagePickerController:(UIImagePickerController *)pickers didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo 
    

    Here is my code that I am using in a current project:
    - (void)imagePickerController:(UIImagePickerController *)pickers didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    	selectedImage = [image retain];
    	[picker dismissModalViewControllerAnimated:YES];
    	[self performSelector:@selector(showUploadImage) withObject:nil afterDelay:0.5];
    }
    
  • tobyaherberttobyaherbert Posts: 19Registered Users
    edited February 2011
    iSDK wrote: »
    I think the better thing to do is set the imageview's image from the method, dismiss the imagepicker, and then perform a void method after 0.5 seconds of the pickerview dismissing which would present the mailcomposer.
    - (void)imagePickerController:(UIImagePickerController *)pickers didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo 
    

    Here is my code that I am using in a current project:
    - (void)imagePickerController:(UIImagePickerController *)pickers didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    	selectedImage = [image retain];
    	[picker dismissModalViewControllerAnimated:YES];
    	[self performSelector:@selector(showUploadImage) withObject:nil afterDelay:0.5];
    }
    

    Thank You iSDK i will try that now.
  • 7twenty77twenty7 Posts: 1,013Registered Users @ @ @ @
    edited February 2011
    iSDK wrote: »
    I think the better thing to do is set the imageview's image from the method, dismiss the imagepicker, and then perform a void method after 0.5 seconds of the pickerview dismissing which would present the mailcomposer.

    I don't think that's the problem. Looks like you're using the actual string "UIImagePickerControllerOriginalImage" rather than the defined variable. That is, you want to do:
    // notice the lack of quotes
    theimageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    

    Two more things--

    Definitely use the callback in your original post--not the one from iSDK. The imagePickerController:didFinishPickingImage:editingInfo callback was deprecated in iOS 3.0.

    Also, relying on specific timings to perform functions (like the call a void method 0.5 seconds after...etc), is destined to fail at some point. You can't rely on specific times on different devices, OS versions, and whatever else might be happening on the device at a particular time. Use the various UIViewController methods to notify you that an even finished (eg, viewDidAppear, etc).
Sign In or Register to comment.