Attach an captured image to an email on clicking a button - iphone

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.

Related

UIImagePickerController After taking 5 images sequentially Terminates Application

//This method Launches the picker to take the picture from camera.
-(IBAction)takeyouphoto:(id)sender
{
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
// Create image picker controller
UIImagePickerController *imagePicker2 = [[UIImagePickerController alloc] init];
// Set source to the camera
imagePicker2.sourceType = UIImagePickerControllerSourceTypeCamera;
// Delegate is self
imagePicker2.delegate = self;
// Allow editing of image ?
imagePicker2.allowsEditing= NO;
// Show image picker
[self presentModalViewController:imagePicker2 animated:YES];
}
}
//This is ImagePicker Delegate method.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
#try {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
{
UIImage *resultimage=nil;
//I am using iOS5, so we can not use NSAutoreleasePool
resultimage=[info objectForKey:UIImagePickerControllerOriginalImage] ;
//This Launches the HUD (Activity Indicator) because ImagePicker ususally takes 5
//seconds to launch image.
[self showHUD:resultimage];
}
}
[picker dismissModalViewControllerAnimated:YES];
}
-(void)showHUD:(UIImage *)resultimage
{
[[Singleton sharedmysingleton] stoptimer];
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = #"Loading Image";
//HUD.detailsLabelText=#"Loading";
//Below call on showWhileExecuting of the MBProgressHuD class has its own NSAutoreleasePool
//Defined in MGProgressHUD class. it also runs the method showimageincell; in separate
//thread.
[HUD showWhileExecuting:#selector(showimagesincell:) onTarget:self withObject:resultimage animated:YES];
}
-(void)showimagesincell:(UIImage *)image
{
appDelegate.tabbarcontroller.tabBar.userInteractionEnabled=NO;
NSError *error;
UIImage *resultImage=[self scale:image toSize:image.size];
//UIImage *resultImage = [[UIImage alloc] initWithCGImage:imgRefCrop scale:1.0 orientation:resultimage.imageOrientation];
//resultimage.imageOrientation
NSData *imagedata=UIImageJPEGRepresentation(resultImage, 0.7);//(resultImage);
UIImage *smallimage=[self scale:image toSize:CGSizeMake(100, 100)];
NSData *smallimagedata=UIImageJPEGRepresentation(smallimage, 0.7);
/* NSString *imagetypeid=[Fetchsavefromcoredata getImagenameandImageidfromdatabase:#"Mobile_ImageType" attributename:#"imageType" predicate:imagetypetxtfield.text];
//write image to document directory
NSString *localImagedir=[photodirpath stringByAppendingPathComponent:selectedvinnumber];
NSString *datetime=[Singleton imagedateandtime];
NSString *imagename=[NSString stringWithFormat:#"%#_%#.png",imagetypeid,datetime];
NSString *localImagePath=[localImagedir stringByAppendingPathComponent:imagename];
[imagedata writeToFile:localImagePath atomically:YES];*/
[self performSelectorOnMainThread:#selector(updatetableview) withObject:nil waitUntilDone:NO];
}
-(void)updatetableview
{
[[Singleton sharedmysingleton] starttimer];
[self viewWillAppear:YES];
}
-(UIImage *)scale:(UIImage *)image toSize:(CGSize)size
{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
//Above is all my code, I have tried to find it on diffrent forums but I have not fixed it yet.
//Any help will be appreciated. Thanks in advance
The white screen issue is fixed by keeping the image returned by the UIIMagePickerController delegate method into an #autoreleasepool (for iOS5). It solved the problem, we can not use NSAutoreleasePool in ARC code.
Here the line of code in didFinishPickingMediaWithInfo: delegate method
UIImage *resultimage=nil;
#autoreleasepool
{
//I am using iOS5, so we can not use NSAutoreleasePool
resultimage=[info objectForKey:UIImagePickerControllerOriginalImage] ;
}
Below the UIImagePickerController delegate method after implementing #autoreleasepool
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
#try {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
{
UIImage *resultimage=nil;
#autoreleasepool
{
//I am using iOS5, so we can not use NSAutoreleasePool
resultimage=[info objectForKey:UIImagePickerControllerOriginalImage] ;
}
//This Launches the HUD (Activity Indicator) because ImagePicker ususally takes 5
//seconds to launch image.
[self showHUD:resultimage];
}
}
[picker dismissModalViewControllerAnimated:YES];
}

Emailing photo taken with ImagePickerController?

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

How to give a name to a saved Image from the imagePickerController

I'm using an imagePickerController to replace my existing images inside my view. The function works within the same IB-file. However I'd like to load the chosen image in another IB-File as well. I think that the solution is to give a name to the image when saved. After it is saved I'd like to call the image (by name) from my memory within my other IB-file.
Here's a snippit of code I'm using within the photopicker IB-file
-(IBAction)setPhoto{
image1.image = fotoView.image;
}
-(IBAction)getCameraPicture:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsImageEditing = YES;
picker.sourceType = (sender == takePictureButton) ? UIImagePickerControllerSourceTypeCamera :
UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController: picker animated:YES];
[picker release];
}
-(IBAction)selectExitingPicture
{
if([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker= [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage : (UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
[picker dismissModalViewControllerAnimated:YES];
fotoView.image = image;
NSData* imdata = UIImagePNGRepresentation ( image );
UIImage* im8 = [UIImage imageWithData:imdata];
UIImageWriteToSavedPhotosAlbum(im8, nil, nil, nil);
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
[picker dismissModalViewControllerAnimated:YES];
}
within my other class I'd like to call this image by means of:
if (#some condition){
UIImage *img = [UIImage imageNamed:#"Name of the image.png"];
[image1 setImage:img];
}
Help is greatly appreciated
Method for image picking and saving it to the directory:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo {
[picker dismissModalViewControllerAnimated:YES];
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath = [NSString stringWithFormat:#"%#/MyName.png",docDir];
NSData *data = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data writeToFile:pngFilePath atomically:YES];
}
Lateron you can call it by using:
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath = [NSString stringWithFormat:#"%#/MyName.png",docDir];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:pngFilePath];

didFinishPickingMediaWithInfo return nil photo

I am working to capture an image that is returned in 4.0 using
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
// MediaType can be kUTTypeImage or kUTTypeMovie. If it's a movie then you
// can get the URL to the actual file itself. This example only looks for images.
//
NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
// NSString* videoUrl = [info objectForKey:UIImagePickerControllerMediaURL];
// Try getting the edited image first. If it doesn't exist then you get the
// original image.
//
if (CFStringCompare((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) {
UIImage* picture = [info objectForKey:UIImagePickerControllerEditedImage];
if (!picture)
picture = [info objectForKey:UIImagePickerControllerOriginalImage];
// **picture is always nil
// info dictionary count = 1
}
}
What is happening is that the info dictionary always returns with a single entry:
{
UIImagePickerControllerMediaType =
"public.image";
which is great, but there is never an image.
I was using a great example from this forum to do this, and I am pretty sure the calls are correct, but never an image.
Thanks in advance!
I know this is many months later, but I struggled with this for hours, and found this same question all over this site and iphonedevsdk.com, but never with a working answer.
To summarize, if the image was picked from the camera roll/photo library it worked fine, but if the image was a new photo take with the camera it never worked. Well, here's how to make it work for both:
You have to dismiss and release the UIImagePickerController before you try to do anything with the info dictionary. To be super-clear:
This DOES NOT work:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
// No good with the edited image (if you allowed editing)
myUIImageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
// AND no good with the original image
myUIImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
// AND no doing some other kind of assignment
UIImage *myImage = [info objectForKey:UIImagePickerControllerEditedImage];
[picker dismissModalViewControllerAnimated:YES];
[picker release];
}
In all of those cases the image will be nil.
However, via the magic of releasing the UIImagePickerController first...
This DOES work:
- (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];
}
Crazy simple, but that's all there is to it.
whilst Matthew Frederick's answer is most popular and has long been the appropriate response, as of iOS 5.0, apple made available dismissViewControllerAnimated:completion:, to replace the now deprecated (as of iOS 6.0) dismissViewControllerAnimated:.
performing the image info dictionary retrieval in the completion block should hopefully make more sense to all.
to take his example from above, it would now look like:
- (void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:^{
// 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];
}];
}
I've tried all above, but no luck on iPad 6.0/6.1 simulator, however i've found that info contains "UIImagePickerControllerReferenceURL" key and here is my code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:NULL];
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
if(NULL == image){
[MyImageLoader loadImageFromAssertByUrl:[info objectForKey:#"UIImagePickerControllerReferenceURL"]
completion:^(UIImage* img){
//img not null here
}];
}else{
//image not null here
}
}
and code for loadImageFromAssertByUrl is:
+(void) loadImageFromAssertByUrl:(NSURL *)url completion:(void (^)(UIImage*)) completion{
ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
[assetLibrary assetForURL:url resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *rep = [asset defaultRepresentation];
Byte *buffer = (Byte*)malloc(rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
UIImage* img = [UIImage imageWithData:data];
completion(img);
} failureBlock:^(NSError *err) {
NSLog(#"Error: %#",[err localizedDescription]);
}];
}
There's also a known bug with some versions of XCode and Mountain Lion. Check your console for this message:
2013-01-17 21:36:34.946 3sure-ios[5341:1803] Named service 'com.apple.PersistentURLTranslator.Gatekeeper' not found. assetsd is down or misconfigured. Things will not work the way you expect them to.
It probably won't work if that message appears. Check your app at the actual iOS device.
Nevetheless, restarting both simulator and XCode seems to fix the issue.
I had the same symptoms, but in my case it turned out I had a category on something in the UIImagePickerController hierarchy that overrode "uuid" or "setUUID"? I guess CoreData needs these methods or it gets very, very confused, and the asset library is all CoreData stuff.
use
[[UIImagePickerController alloc] init];
instead of
[[UIImagePickerController alloc] initWithNibName:(NSString *) bundle:(NSBundle *)];
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:NULL];
//Image picker for nil value
UIImage* selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSLog(#"my Info :: %#", info);
if(NULL == selectedImage){
UIImage * selectedImage = [PhotoViewController loadImageFromAssertByUrl:[info objectForKey:#"UIImagePickerControllerReferenceURL"]
completion:^(UIImage * selectedImage){
//img not null here
NSLog(#"img2 ::: %#", selectedImage);
uploadImg.image = selectedImage;
[self.view addSubview:PermissionView];
}];
}else{
//image not null here
NSLog(#"Up IMG00 :: %#", uploadImg.image);
NSLog(#"Sel IMG00 :: %#", selectedImage);
uploadImg.image = [selectedImage retain];
NSLog(#"Up IMG22 :: %#", uploadImg.image);
NSLog(#"Sel IMG22 :: %#", selectedImage);
}
}
+(UIImage *) loadImageFromAssertByUrl:(NSURL *)url completion:(void (^)(UIImage*)) completion{
__block UIImage* img;
ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
[assetLibrary assetForURL:url resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *rep = [asset defaultRepresentation];
Byte *buffer = (Byte*)malloc(rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
img = [UIImage imageWithData:data];
completion(img);
NSLog(#"img ::: %#", img);
} failureBlock:^(NSError *err) {
NSLog(#"Error: %#",[err localizedDescription]);
}];
return img;
}
Following Steps needs to be followed:
1) Make UIActionSheet
2) On selection necessary source type - load resrouces
3) didFinishPickingMediaWithInfo - set image from image Dictionary recieved
- (IBAction)actionButtonUpload:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self
cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Camera",#"Photo Library", nil];
[actionSheet showInView:self.view];
}
#pragma mark -- UIActionSheet Delegate
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
UIAlertView *alert;
switch (buttonIndex) {
case 0:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSLog(#"No camera!");
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
} else {
alert = [[UIAlertView alloc]initWithTitle:#"Alumnikonnect" message:#"Camera is not available, please select a different media" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}
break;
case 1:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
} else {
alert = [[UIAlertView alloc]initWithTitle:#"Alumnikonnect" message:#"Photolibrary is not available, please select a different media" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}
break;
}
}
#pragma mark - Image picker delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
self.imageEventPic.layer.cornerRadius = self.imageEventPic.frame.size.height /2;
self.imageEventPic.layer.masksToBounds = YES;
self.imageEventPic.layer.borderWidth = 0;
self.imageEventPic.layer.borderWidth = 1.0f;
self.imageEventPic.layer.borderColor = [UIColor colorWithRed:0.212 green:0.255 blue:0.302 alpha:1.000].CGColor;
self.labelUploadPic.hidden = YES;
self.imageEventPic.image = info[UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
-(UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

UIImageWriteToSavedPhotosAlbum Problem

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.