In my app I want to:
a.) Present camera to user to take picture
b.) Create email and attach picture taken
I figured out how to take the picture. But, I don't know how to attach it to the email. In the examples I've seen the filename is known. This is one example.
picker.mailComposeDelegate = self;
[picker setSubject:#"I have a pencil for you"];
UIImage *roboPic = [UIImage imageNamed:#"RobotWithPencil.jpg"];
NSData *imageData = UIImageJPEGRepresentation(roboPic, 1);
[picker addAttachmentData:imageData mimeType:#"image/jpg" fileName:#"RobotWithPencil.jpg"];
NSString *emailBody = #"This is a cool image of a robot I found. Check it out!";
[picker setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:picker animated:YES];
I am getting the image from the callback method and it does get stored in the camera roll.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[picker dismissModalViewControllerAnimated:TRUE];
[picker release];
}
But from there, I'm not sure how to attach the image to the email? Mainly because I don't know what the filename is when a photo is added to the camera roll when the UIImagePickerController camera source is used to obtain it?
So, I guess I need to either:
a.) Find a way to get the name of the image file once it is saved OR
b.) Another way to attach UIImage data as an email attachment.
Any help appreciated.
maybe I don't understand what you are trying to do UI wise, but why would this not work from your code provide:
EDIT: (changed code to use a delegate)
- (void)imagePickerController:(UIImagePickerController *)imagepicker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
if([delegate respondsToSelector:#selector(didSelectImage:)])
[delegate didSelectImage:image];
}
this goes in your header file (usually above your class declaration):
#protocol PostHelperDelegate <NSObject>
#optional
- (void)DimissModal;
//image data for image selected
- (void)DidSelectImage:(UIImage*)image;
#end
also you need to create this property in your class
#property(nonatomic,assign)id<PostHelperDelegate>* delegate; //don't forgot to synthizie
then in the class implementing the delegate you will assign the delegate your class:
Poster = [[delegateclass alloc] init];
Poster.delegate = self;
then just add the delegate function:
- (void)DidSelectImage:(UIImage*)image
{
//handle image here
}
I simplify this quite a bit, let me know if you have any trouble getting it working
http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/
BEFORE UPDATE:
personally I would use a delegate to notify that the image has been picked and than launch the mail picker, but that is another thing. As far as the UIImage, I don't know a way or have never found a way to name/or get a name from the UIImagePicker as I do not believe they are named anything meaningful. Any questions on the delegate or further explanation let me know.
use bellow code
-(void)yourmethodname_clicked{
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
}
else{
UIAlertView *altnot=[[UIAlertView alloc]initWithTitle:#"Camera Not Available" message:#"Camera Not Available" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[altnot show];
[altnot release];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
MFMailComposeViewController *mailpicker = [[MFMailComposeViewController alloc] init];
[mailpicker.navigationBar setTintColor:[UIColor blackColor]];
mailpicker.mailComposeDelegate = self;
[mailpicker setSubject:#"set your subject"];
NSArray *toRecipients = [NSArray arrayWithObjects: nil];
[mailpicker setToRecipients:toRecipients];
UIImage *picture = [info objectForKey:#"UIImagePickerControllerOriginalImage"]];;
NSData *imageData = UIImagePNGRepresentation(picture);
[mailpicker addAttachmentData:imageData mimeType:#"image/png" fileName:#"set your file name"];
NSString *emailBody = #"set your body string";
[mailpicker setMessageBody:emailBody isHTML:YES];
[mailpicker setSubject:#"set your subject"];
mailpicker.title = #"Email";
[self presentModalViewController:mailpicker animated:YES];
[mailpicker release];
}
may this help you
Related
I have captured image from iphone using the below UIImagePickerController delegate method.
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
self.myImageView.image = image;
// [self performSelector:#selector(emailButtonPressed:) withObject:image afterDelay:1.0];
[self dismissModalViewControllerAnimated:YES];
}
See above emailButtonPressed method it was called by self. I want to call this in a button action.
I wrote the code below for emailButtonPressed.
- (void)emailButtonPressed:(UIImage *)image
{
MFMailComposeViewController *mailview=[[MFMailComposeViewController alloc]init]; mailview.navigationBar.tintColor=[UIColor colorWithRed:55/255.0 green:190/255.0 blue:55/255.0 alpha:1];
mailview.mailComposeDelegate=self;
// NSMutableString *subject=[NSMutableString stringWithFormat:#"%#",#"Testing"];
[mailview setSubject:#"Picture from my iPhone!"];
// NSString *email_new=#"";
[mailview setMessageBody:#"Description" isHTML:NO];
NSData *imageData = UIImagePNGRepresentation(image);
[mailview addAttachmentData:imageData mimeType:#"image/png" fileName:#"ImageName"];
[self presentModalViewController:mailview animated:YES];
}
sorry for any mistakes in my code.
Modify your emailButtonPressed method as follows,
- (void)emailButtonPressed //removed the param
{
UIImage *image = self.myimageview.image; //or set some other param as image = self.image; whichever you set in picker delegate method
MFMailComposeViewController *mailview=[[MFMailComposeViewController alloc]init]; mailview.navigationBar.tintColor=[UIColor colorWithRed:55/255.0 green:190/255.0 blue:55/255.0 alpha:1];
mailview.mailComposeDelegate=self;
// NSMutableString *subject=[NSMutableString stringWithFormat:#"%#",#"Testing"];
[mailview setSubject:#"Picture from my iPhone!"];
// NSString *email_new=#"";
[mailview setMessageBody:#"Description" isHTML:NO];
NSData *imageData = UIImagePNGRepresentation(image);
[mailview addAttachmentData:imageData mimeType:#"image/png" fileName:#"ImageName"];
[self presentModalViewController:mailview animated:YES];
}
Keep this method as is,
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
self.myImageView.image = image; //instead of this, you can create an #property for image in .h file and assign to that also here.
[self dismissModalViewControllerAnimated:YES];
}
Assuming that you have declared your email button as,
UIButton *emailbutton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //or any other way
Now add this method as your button target immediately after that line,
[emailbutton addTarget:self action:#selector(emailButtonPressed) forControlEvents:UIControlEventTouchUpInside];
Now on tap of emailbutton whatever image you have set in self.myimageview.image in UIImagePickerController delegate will be sent as an attachment.
I have used the following code to get images from album.
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[imagePicker setDelegate:self];
[self presentModalViewController:imagePicker animated:YES];
and
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:YES];
[picker release];
// Edited image works great (if you allowed editing)
//myUIImageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
// AND the original image works great
//myUIImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
// AND do whatever you want with it, (NSDictionary *)info is fine now
//UIImage *myImage = [info objectForKey:UIImagePickerControllerEditedImage];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[customImageView setImage:image];
customImageView.contentMode = UIViewContentModeScaleAspectFill;
}
But i didn't get any response from device.
I have gone through all SO answers but its not working for me.
Can anyone please tell me how to get images from the photos album.
Remove your release statement from didFinishPickingMediaWithInfo method. Write it as
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
I am succesfully saving an image to my app after the user takes a picture. What I want to do later is, when the user comes back to the app I want them to be able to email the photo as an attachment. I am not having any luck getting the data from the app converted to an image so I can add as an attachment. Can someone point me in the right direction please. Here is where I save the image after they have taken a picture.
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//here is the image returned
app.aImage2 = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *imageData = UIImagePNGRepresentation( app.aImage2 );
NSString * savedImageName = [NSString stringWithFormat:#"r%#aImage2.png",app.reportNumber];
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * dataFilePath;
dataFilePath = [documentsDirectory stringByAppendingPathComponent:savedImageName];
[imageData writeToFile:dataFilePath atomically:YES];
[self dismissModalViewControllerAnimated:YES];
}
And here is where I need to attach it.
//this is inside my method that creates an email composer view
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self; // <- very important step if you want feedbacks on what the user did with your email sheet
//how would i attach the saved image from above?
This includes code that Mike mentions here:
How to add a UIImage in MailComposer Sheet of MFMailComposeViewController
Also, other portions are lifted from Sagar Kothari's answer here:
Sending out HTML email with IMG tag from an iPhone App using MFMailComposeViewController class
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Dismiss image picker modal.
[picker dismissModalViewControllerAnimated:YES];
if ([MFMailComposeViewController canSendMail]) {
// Create a string with HTML formatting for the email body.
NSMutableString *emailBody = [[NSMutableString alloc] initWithString:#"<html><body>"];
// Add some text to it.
[emailBody appendString:#"<p>Body text goes here.</p>"];
// You could repeat here with more text or images, otherwise
// close the HTML formatting.
[emailBody appendString:#"</body></html>"];
NSLog(#"%#", emailBody);
// Create the mail composer window.
MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
emailDialog.mailComposeDelegate = self;
// Image to insert.
UIImage *emailImage = [info objectForKey:UIImagePickerControllerOriginalImage];
if (emailImage != nil) {
NSData *data = UIImagePNGRepresentation(emailImage);
[emailDialog addAttachmentData:data mimeType:#"image/png" fileName:#"filename_goes_here.png"];
}
[emailDialog setSubject:#"Subject goes here."];
[emailDialog setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:emailDialog animated:YES];
[emailDialog release];
[emailBody release];
}
}
I'm using the UIImagePickerController in iOS 4.2.1 on an iPhone 3Gs. I've previously used the deprecated method
- (void)imagePickerController: didFinishPickingImage: editingInfo:
without a problem. I have another app using the new didFinishPickingMediaWithInfo API in another app, and the method is never getting called by the picker once media is chosen.
//MyViewController.h
#interface MyViewController : UIViewController < UIImagePickerControllerDelegate, UINavigationControllerDelegate>
//MyViewController.m
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController new] autorelease];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
picker.videoQuality = UIImagePickerControllerQualityTypeHigh;
picker.allowsEditing = NO;
[self presentModalViewController:picker animated:TRUE];
}
- (void)imagePickerController:(UIImagePickerController *)picker imagePickerController:didFinishPickingMediaWithInfo:(NSDictionary *)editingInfo{
//**NEVER CALLED**
}
you have
- (void)imagePickerController:(UIImagePickerController *)picker imagePickerController:didFinishPickingMediaWithInfo:(NSDictionary *)editingInfo
where you probably want
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
You have this:
- (void)imagePickerController:(UIImagePickerController *)picker imagePickerController:didFinishPickingMediaWithInfo:(NSDictionary *)editingInfo
Look like you repeated the 'imagePickerController:' part of that method.
Putting the picker in the autorelease pool may be your problem -- it probably doesn't stick around long enough to call its delegate. Retain it instead:
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
And then in the delegate you can release it:
[picker dismissModalViewControllerAnimated:YES];
[picker release];
It may be that this line:
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
is returning false. This happens if the user's photo library is empty; this would be true on the iPhone simulator, for example.
EDIT: As the other examples show, you've also mistyped the delegate method. It should be:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
This is program that i have used to upload video to webservice through iOS 4.2 on 3g
-(void)uploadeVideoClicked{
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
ipc.allowsEditing=NO;
ipc.videoQuality = UIImagePickerControllerQualityTypeMedium;
ipc.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:ipc.sourceType];
ipc.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie];
ipc.delegate = self;
[self presentModalViewController:ipc animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSMutableDictionary *infoDict = [[NSMutableDictionary alloc]init];
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.image"]){
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"" message:#"You Select a image Please select Movie" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[myAlertView show];
[myAlertView release];
} else if ([mediaType isEqualToString:#"public.movie"]){
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
mAppDelegate.uploadType = #"Video";
NSData *webData = [NSData dataWithContentsOfURL:videoURL];
[infoDict setValue:webData forKey:#"VideoUrl"];
[infoDict setValue:[[mAppDelegate.userInfoArray objectAtIndex:1]valueForKey:#"user_id"] forKey:#"user_id"];
//Call webService to upload video ;
}
[picker dismissModalViewControllerAnimated:YES];
[infoDict release];
}
i try to save a photo from camera after take a photo with a button .
here is my codes:
-(IBAction)savePhoto{
UIImageWriteToSavedPhotosAlbum(img.image, nil, nil);
}
-(IBAction)takePic {
ipc = [[UIImagePickerController alloc]init];
ipc.delegate = self;
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:ipc animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
img.image = [info objectForKey:UIImagePickerControllerOriginalImage];
[[picker parentViewController]dismissModalViewControllerAnimated:YES];
[picker release];
}
but i dont know why doesnt save anything !
I suspect your image is released before you save it:
img.image = [info objectForKey:UIImagePickerControllerOriginalImage];
The line above returns an autoreleased object. I suggest you retain the image as shown below and then release it when you have saved the image.
img.image = [[info objectForKey:UIImagePickerControllerOriginalImage] retain];
Call [self savePhoto] in -imagePickerController:didFinishPickingMediaWithInfo: after retrieving the image.