Chosen Image Is Not Showing - ios

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

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
}

UIImagePickerController: switching source types does not work

I have a UIImagePickerController with a custom overlay view. The view controller opens originally with the camera as the source type.But there is a button that switches the source type to photo album. If they enter the album mode, they can hit cancel to switch the source type back to camera.
This works for the first round. But if they press the album button a second time (after having already entered album mode and canceled out of it), the screen loads the white background but doesn't load the user's photo library. The app is then stuck.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
_activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
ipc.showsCameraControls = NO;
[[NSBundle mainBundle] loadNibNamed:#"CameraView" owner:self options:nil];
self.overlayView.frame = ipc.cameraOverlayView.frame;
ipc.cameraOverlayView = self.overlayView;
self.overlayView = nil;
CGSize screenBounds = [UIScreen mainScreen].bounds.size;
int cameraViewHeight;
int adjustedYPosition;
/*Center Uiimagepickercontroller in screen */
if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
cameraViewHeight = screenBounds.width * 1.333;
adjustedYPosition = (screenBounds.height - cameraViewHeight) / 2;
NSLog(#"portrait screenbounds width: %f, screenbounds height: %f", screenBounds.width, screenBounds.height);
CGAffineTransform translate = CGAffineTransformMakeTranslation(0, adjustedYPosition);
ipc.cameraViewTransform = translate;
NSLog(#"in portrait value is %d", adjustedYPosition);
}else{
//landscape mode
cameraViewHeight = screenBounds.height * 1.333;
adjustedYPosition = (screenBounds.width - cameraViewHeight) / 2;
CGAffineTransform translate = CGAffineTransformMakeTranslation(0, adjustedYPosition);
ipc.cameraViewTransform = translate;
}
ipc.showsCameraControls = NO;
ipc.tabBarController.tabBar.hidden = YES;
}else{
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
[self presentViewController:ipc animated:YES completion:nil];
[_activityIndicator stopAnimating];
}
- (IBAction)albumView:(id)sender {
[ipc setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
/*navigate to previous tab */
if(picker.sourceType == UIImagePickerControllerSourceTypeCamera){
[picker dismissViewControllerAnimated:YES completion:nil];
LeafsnapTabBarController * tabBarController = (LeafsnapTabBarController*)self.tabBarController;
[self.tabBarController setSelectedIndex:tabBarController.previousTabIndex];
}else{
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
}
I'm not sure why this works, so if someone can explain it I will mark their response as correct. But this is my solution:
- (IBAction)albumView:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker.view setFrame:self.view.frame];
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[imagePicker setDelegate:self];
[ipc presentViewController:imagePicker animated:YES completion:nil];
}
- (IBAction)cancel_IPC:(id)sender {
[self imagePickerControllerDidCancel:ipc];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
if(picker.sourceType == UIImagePickerControllerSourceTypeCamera){
LeafsnapTabBarController * tabBarController = (LeafsnapTabBarController*)self.tabBarController;
[self.tabBarController setSelectedIndex:tabBarController.previousTabIndex];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage* origImage = [info objectForKey:UIImagePickerControllerOriginalImage];
/*do whatever you need to do with the img*/
[picker dismissViewControllerAnimated:YES completion:nil];
/*dismiss the original UIImagePickerController in background */
if(picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary){
[ipc dismissViewControllerAnimated:YES completion:nil];
}
[mLocationManager stopUpdatingLocation];
}

How to overlay camera capture image?

I am replacing the part of image by capture image from camera. i need to perform the activity like this app in my application. https://itunes.apple.com/in/app/replace-my-face-funny-girls/id794033892?mt=8
I need a way of how can i take picture from background view with super view image and save the new generated image with merging of both image.
Please suggest me solution.
The simple work i did
- (void)viewDidLoad {
[super viewDidLoad];
self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.delegate = self;
self.picker.allowsEditing = NO;
self.picker.showsCameraControls = NO;
self.picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
CGRect overlayRect = CGRectMake(0, 0, self.imageView.frame.size.width,self.imageView.frame.size.height);
UIView *overlayView = [[UIView alloc] initWithFrame:overlayRect];
UIImageView *capture_image=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.imageView.frame.size.width,self.imageView.frame.size.height)];
capture_image.image=[UIImage imageNamed:#"1.jpg"];
[overlayView addSubview:capture_image];
[self.picker setCameraOverlayView:overlayView];
[self.navigationController presentViewController:self.picker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Please also suggest me if there is any repository working like this approach.
About capturing the image: you might want to look at FastttCamera, which allows you to set the camera view to your defined image view and crops the image accordingly.
Good luck!

UIImagePickerController causing crash

I'm using ios 8.4. When debugging an app, and I present a UIImagePickerController, xcode loses connection with the iphone. This wasn't a problem before.
Sometimes it will bring up the image picker... but then when I save an image, there'll be a crash.
Is anyone else experiencing this? How to fix?
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.editing = NO;
imagePickerController.delegate = self;
imagePickerController.showsCameraControls=YES;
[self presentViewController:imagePickerController animated:YES completion:nil];
try this code
- (IBAction)galleryButtonPressed:(id)sender
{
UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
pickerController.allowsEditing = YES;
pickerController.delegate = self;
[self presentViewController:pickerController animated:YES completion:NULL];
}
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:NULL];
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
if (image == nil)
image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Do something with the image
[self.imageView setImage:image];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:NO completion:nil];
}

move and scale bounces back

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.

Resources