My application is about clicking on a button with background image named "uploadphoto1.png". When I click on the button, it shows me an ActionSheet letting me choose to take a picture with the camera or taking a picture from the library.
When, for example, I choose a picture from the camera, it lets me select a picture and show the picture in an imageView and it change the background image of the button from upload.png to save.png.
The problem now that when I click on the button with background Image "save1.png" it shows me the actionSheet.
I want to hide the actionSheet.
Here is the code:
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *buttonpicture =[UIImage imageNamed:#"uploadphoto1.png"];
[ButtonPicture setBackgroundImage:buttonpicture forState:UIControlStateNormal];
[ButtonPicture setFrame:CGRectMake(173, 269, 140, 30)];
}
- (IBAction)ButtonPicture:(UIButton *)sender
{
NSLog(#"Button Picture Pressed");
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:#"Photo" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Picture From Camera",#"Picture From Gallery",nil];
[actionSheet setTag:1001];
[actionSheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex)
{
case 0:
{
// Take a picture from camera
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
}
break;
case 1:
{
// take a picture from gallery photos
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self.view addSubview:toolbar];
[self presentModalViewController:picker animated:YES];
}
break;
default:
break; }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[ButtonPicture setBackgroundImage:[UIImage imageNamed:#"save1.png"] forState:UIControlStateNormal];
[picker dismissModalViewControllerAnimated:YES];
imageview.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//[actionSheet showInView:Nil];
}
#interface YourViewController
{
BOOL imageChanged;
}
- (void)viewDidLoad
{
[super viewDidLoad];
imageChanged = NO;
UIImage *buttonpicture =[UIImage imageNamed:#"uploadphoto1.png"];
[ButtonPicture setBackgroundImage:buttonpicture forState:UIControlStateNormal];
[ButtonPicture setFrame:CGRectMake(173, 269, 140, 30)];
}
- (IBAction)ButtonPicture:(UIButton *)sender
{
NSLog(#"Button Picture Pressed");
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:#"Photo" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Picture From Camera",#"Picture From Gallery",nil];
[actionSheet setTag:1001];
if (!imageChanged)
[actionSheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex)
{
case 0:
// Take a picture from camera
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
break;
case 1:
// take a picture from gallery photos
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self.view addSubview:toolbar];
[self presentModalViewController:picker animated:YES];
break;
default:
break;
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[ButtonPicture setBackgroundImage:[UIImage imageNamed:#"save1.png"] forState:UIControlStateNormal];
imageChanged = YES;
[picker dismissModalViewControllerAnimated:YES];
imageview.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
There are various ways this could be handled. One way is to set a boolean flag when you don't want tapping on the button to bring up the actionSheet. Then, in - (IBAction)ButtonPicture:(UIButton *)sender you can check the boolean flag to decide whether or not to create the actionSheet and show it.
Related
I have built an camera and library app where I am choosing image from my library and showing it to an imageView. But the problem that is happening is after choosing the image is not showing in imageView.
Here is my code.
#interface ViewController ()
{
BOOL isImageselected;
UIImageView *myImage;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Device has no camera"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[myAlertView show];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)addPhotoButton:(id)sender {
[UIView animateWithDuration:0.3 animations:^{
// animation here
self.pickerView.frame = CGRectMake(0.0, 443.0, 320.0, 125.0);
}];
}
- (IBAction)cameraButton:(id)sender {
[UIView animateWithDuration:0.3 animations:^{
// animation here
self.pickerView.frame = CGRectMake(0.0, 568.0, 320.0, 125.0);
}];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
- (IBAction)uploadImageButton:(id)sender {
[UIView animateWithDuration:0.3 animations:^{
// animation here
self.pickerView.frame = CGRectMake(0.0, 568.0, 320.0, 125.0);
}];
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];
myImage.image = chosenImage;
isImageselected = YES;
if (isImageselected == YES) {
myImage = [[UIImageView alloc]initWithFrame:CGRectMake(8.0, 8.0, 95.0, 95.0)];
self.addPhotoButton.frame = CGRectMake(106.0, 8.0, 95.0, 95.0);
}
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
#end
I think the problem is happening where I am creating the imageView dynamically but I am not getting the point what is wrong.
Problem is that you assigning image to UIImageView object myImage before creating myImage.
Try to place the line
myImage.image = chosenImage;
After this line:
myImage = [[UIImageView alloc]initWithFrame:CGRectMake(8.0, 8.0, 95.0, 95.0)];
This because after adding the selected photo to myImage you are re allocating myImage, this create new object of myImage. Try this
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
isImageselected = YES;
if (isImageselected == YES) {
myImage = [[UIImageView alloc]initWithFrame:CGRectMake(8.0, 8.0, 95.0, 95.0)];
....
myImage.image = chosenImage; }
You cant not assign object before creating it, so just change your code a little bit like
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
isImageselected = YES;
if (isImageselected == YES) {
myImage = [[UIImageView alloc]initWithFrame:CGRectMake(8.0, 8.0, 95.0, 95.0)];
[self.view addSubView:myImage];
self.addPhotoButton.frame = CGRectMake(106.0, 8.0, 95.0, 95.0);
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
myImage.image = chosenImage;
}
[picker dismissViewControllerAnimated:YES completion:NULL];
}
I tried to upload the two images one by one from UIImagePicker. UIImagePicker working good for single image. With UIImagePicker input i uploaded those photos into web server. For single image uploading into web server its working fine, But instead of two images one image is saving two times. This is the first time I am working on UIImagePicker. I don't know exactly where should I do changes to make it for uploading two images.
- (void)takePhoto:(UIButton *)sender {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Device has no camera"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alertView show];
}
else{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
}
- (void)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 {
chosenImage = info[UIImagePickerControllerEditedImage];
chosenImage1=info[UIImagePickerControllerEditedImage];
chosenImage2=info[UIImagePickerControllerEditedImage];
imageView.image = chosenImage;
frstCmpImgView.image= chosenImage1;
scndCmpImgView.image=chosenImage2;
if ([self.navigationItem.title isEqual: #"CLOTHING"]) {
NSLog(#"cloth page");
[self clothview];
}
else if([self.navigationItem.title isEqual: #"FACE PHOTO"]){
[self hideButtns];
NSLog(#"face page");
}
else{
//----compare object--
if ([cmparObjLabel.text isEqualToString:#"Choose the 1st Object"]) {
[self afterUploadFrstCmpObj];
}
else if ([cmparObjLabel.text isEqualToString:#"Choose the 2nd Object"])
{
[self afterUploadScndCmpObj];
}
}
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
You have to use array for adding image in each and every click of take photo because image picker only fetch image one at a time , another option You can user ELCAlbumPicker to select multiple image and add these image over array and use them.
I got a error on Presenting FDTake (Github-Link) FROM a PRESENTED TableViewController:
Warning: Attempt to present UIImagePickerController: 0x14454a880 on NavigationViewController: 0x14452f900 whose view is not in the window hierarchy!
NavigationViewController is a subclass of UINavigationController. It's from REMenu (https://github.com/romaonthego/REMenu).
Any Idea how I can solve it? I already wrote a Issue to the repository but nobody answered, so I thought maybe you guys know why.
I have never used that library though I can share my code for the same functionality with you. Feel free to use it whereever you were using FDTake. Don't forget to set Actionsheet and UIImagePickerController Delegates.
-(IBAction)chooseProfilePicture:(id)sender
{
UIActionSheet *filterActionSheet = [[UIActionSheet alloc] initWithTitle:#"Select Operation"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Choose from gallery", #"Take Photo", nil];
[filterActionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
imgPicker.delegate = self;
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
switch (buttonIndex) {
case 0:
[self.navigationController presentModalViewController:imgPicker animated:YES];
break;
case 1:
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
imgPicker.sourceType =
UIImagePickerControllerSourceTypeCamera;
imgPicker.mediaTypes = #[(NSString *) kUTTypeImage];
imgPicker.allowsEditing = NO;
[self presentViewController:imgPicker
animated:YES completion:nil];
//_newMedia = YES;
}
break;
default:
break;
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo
{
[picker dismissModalViewControllerAnimated:YES];
//Do what you want with the captured/selected image
}
It's that simple. Not really a need for using third party libs for that. Not an answer to your question but it is relevant in some fashion.
I have 2 UIViewControllers in my app.
In the 1st viewController, i have a UIButton in a toolbar.
When i click on the button, it displays me a UIActionSheet to select whether to take a picture from camera or from gallery.
When i choose, for example, taking picture from gallery and i choose a picture, it displays the picture in the 1st viewController.
What i want is to take a picture from gallery photos or from camera and to go to the 2nd viewController and display the image in the 2nd viewController.
I searched a lot and tried so much codes but it doesn't work.
Here is the code of ViewController.m
- (IBAction)PictureButton:(UIButton *)sender {
NSLog(#"Click button take picture in toolbar view 1");
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Photo"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Picture From Camera", #"Picture From Gallery", nil];
[actionSheet setTag:1001];
[actionSheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex)
{
case 0: {
// Take a picture from camera
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
}
break;
case 1: {
// take a picture from gallery photos
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self.view addSubview:toolbar];
[self presentModalViewController:picker animated:YES];
}
break;
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
In my second view i created a UIImageView here is my code in SecondViewController.h
#interface SecondViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIImageView *imageButton;
#end
Can You help me please.
Sorry for my bad English and thanks
It seems you want to select image at FirstViewController and display it in SecondViewContoller. However, you did not do anything to switch to SecondViewController and pass your selected image to it in ViewController.m.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondViewController.imageButton.image = image;
[self.navigationController pushViewController:secondViewController animated:YES];
}
As you can see in this video it bounces back whenever I try to crop or move the crop box. I can't find any way to do this and I think my code is correct. How am I suppose to do this?
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Device has no camera"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[myAlertView show];
}
}
- (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];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
If image is too far away use this method to animate it back:
[UIView animateWithDuration: animations: completion:]
animations: is a block and it will change e.g CGRects over a duration, if you want to change the image position you add [image setRect:(CGRectMake(orginPosition.x, orginPosition.y, size.width, size.height))];
completion: is a block but isn't required.