move and scale bounces back - ios

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.

Related

How to save the image captured using Camera and save in another ViewController?

How to save the image captured using Camera and save in another ViewController ?
I am making a Photo Gallery App. But i dont know how to save the Captured image and show it in collection view in another viewcontroller. Currently i am using UIImagePickerController to access iphone camera and library .Can anyone help me to do that.
Here is my code.
Thanks in advance
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (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];
}
- (IBAction)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 {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)viewDidLoad {
[super viewDidLoad];
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.
}
#end
Try This
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
// Save image
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
[picker release];
}
you can achieve it by adding NSNotificationcenter.
Class A - Where you select the image from camera
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
[[NSNotificationCenter defaultCenter] postNotificationName: #"GotNewImage" object: chosenImage];
}
Class B - Where you want to pass image after selection and show on collection view. Add Nsnotificationcenter for this class in Viewdidload
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(GotNewImage:) name:#"GotNewImage" object:nil];
//To get the image on Class B
-(void)GotNewImage:(NSNotification*)notification
{
UIImage * newImage = (UIImage*) notification.object;
//Now do whatever you want to do with your image...
//Add in collection view and reload the collection view
}

Chosen Image Is Not Showing

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];
}

picture taken with camera doesn't resize in uiimageview

My code
- (IBAction)takePhoto:(UIButton *)sender {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
UIImagePickerController *picker = [UIImagePickerController new];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"ERROR!" message:#"Device has no camera!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
and the delegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.userImage.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Well, thats pretty simple and basic. My problem is that after assigning my image to the UIImageView I have (which has a pinned/fixed width and height) instead of resizing itself to the size of the uiimageview it stretches throughout my whole view, working more as a background that an image placed where it should be. Again, the width and height of my uiimageview has been pinned. Can anybody help? thanks
The contentMode of your UIImageView should be UIViewContentModeScaleAspectFit, instead of UIViewContentModeCenter as it seems to be.
If you need to change the content mode on the fly, just add this line:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.userImage.image = chosenImage;
// Add this line
self.userImage.contentMode = UIViewContentModeScaleAspectFit;
[picker dismissViewControllerAnimated:YES completion:NULL];
}

Upload two images from UIImagePicker

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.

Hide ActionSheet iOS

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.

Resources