Use Camera & Photo Album - ios

I am trying to have a button that is meant to open the camera and a button that is meant to open camera roll. Here is my code:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (IBAction)getCameraPicture:(id)sender{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = NO;
picker.sourceType = (sender == takePictureButton) ?
UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
- (IBAction)selectExistingPicture {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sorry" message:#"Device does not support photo library" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
}
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
imageView.image = image;
}
- (void)viewDidLoad
{
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
takePictureButton.hidden = YES;
}
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Any ideas on why it's not working? Everything is linked but the buttons do nothing
Thanks
EDIT
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIImagePickerControllerDelegate> {
IBOutlet UIButton *takePictureButton;
IBOutlet UIImageView *imageView;
}
#property (nonatomic, retain) IBOutlet UIImageView *imageView;
#property (nonatomic, retain) IBOutlet UIButton *takePictureButton;
- (IBAction)getCameraPicture:(id)sender;
- (IBAction)selectExistingPicture;
#end

Just add :
[self presentViewController:picker animated:YES completion:nil];
at the end of your 'getCameraPicture:' and in your 'selectExistingPicture:'.
And you also might want to add the 'UIImagePickerControllerDelegate' to your view controller's interface so the 'didFinishPickingImage' gets called.
Edit :
You should replace your :
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo;
with
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
beccause that method is long deprecated.
Use it like this :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Code from http://www.appcoda.com/ios-programming-camera-iphone-app/.

Related

How to keep an image in a UIImageView when switching between multiple UIViewControllers

I'm trying to write an extremely basic Xcode (ver. 5) project application for the iPhone5 that loads an image from the photo library into an UIImageView in the start up UIViewController and keeps that image in the UIImageView (or returns it) when switching between other view controller panels. The problem I am having is that when I return from other UIViewController windows to the main view controller with my UIImageView, the loaded image that was previously displayed is gone and I end up having to reload it into the UIImageView. How can I make this behave where the image remains in the UIImageView after I return from other UIViewController windows?
Implementation File...
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (IBAction)btnClick:(id)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
{
modBuffer = info[UIImagePickerControllerEditedImage];
self.imageView.image = modBuffer;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
Header file...
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
UIImage* modBuffer;
}
#property (strong, nonatomic) IBOutlet UIImageView* imageView;
- (IBAction)btnClicked:(id)sender;
I think you need to make property for UIImage (nonatomic,retain) and then u should try to do it.

UIImagePickerController and blank ImageView

I have a problem which is really strange : I have a TableViewController with 2 pictures and 2 buttons. The user could change the picture with the button.
The problem is when the user click on the button, the UIActionSheet opens, the user chooses the "Photo from library" and then select a photo in the library. The UIActionSheet closes but there is no picture in my ImageView... It's still blank... (My ImageView are linked to # property).
Here is my code :
.H file :
#import <UIKit/UIKit.h>
#interface EditUserTableViewController : UITableViewController <UITextFieldDelegate, UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
#property (weak, nonatomic) IBOutlet UIImageView *avatar;
#property (weak, nonatomic) IBOutlet UIImageView *coverPhoto;
- (IBAction)changeImage:(id)sender;
#end
.M file :
#import "EditUserTableViewController.h"
typedef NS_ENUM(NSInteger, THPhotoType)
{
THPhotoTypeCoverPhoto = 10,
THPhotoTypeAvatarPhoto = 20
};
#interface EditUserTableViewController ()
#property (nonatomic, assign) THPhotoType selectedPhotoType;
#end
#implementation EditUserTableViewController
#synthesize coverPhoto, avatar;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)changeImage:(id)sender {
if ([sender isKindOfClass:[UIButton class]]) {
UIButton *button = sender;
_selectedPhotoType = button.tag;
}
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:#"Select Picture" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"From Library", #"From Camera", nil];
[sheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex < 2) {
UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
UIImagePickerControllerSourceType type;
switch (buttonIndex) {
case 0:
type = UIImagePickerControllerSourceTypePhotoLibrary;
break;
case 1:
type = UIImagePickerControllerSourceTypeCamera;
break;
default:
break;
}
imagePicker.sourceType = type;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:^{ }];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
switch (_selectedPhotoType) {
case THPhotoTypeCoverPhoto:
coverPhoto.image = [info objectForKey:UIImagePickerControllerOriginalImage];
break;
case THPhotoTypeAvatarPhoto:
avatar.image = [info objectForKey:UIImagePickerControllerOriginalImage];
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:^{ }];
}
#end
It looks like problem in setting image to your image view.Try this
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
switch (_selectedPhotoType) {
case THPhotoTypeCoverPhoto:
self.coverPhoto.image = [info objectForKey:UIImagePickerControllerOriginalImage];
break;
case THPhotoTypeAvatarPhoto:
self.avatar.image = [info objectForKey:UIImagePickerControllerOriginalImage];
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:^{ }];
}
Ok I'm a "%^&*&^"...
It was a problem of tag for my buttons...
Problem is solved !

Displaying image in a new view controllers UIImageView

I'll post my code first and then I'll explain the problem
RootViewController.h its called SpViewController.h
#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import "secondViewController.h"
#interface spViewController : UIViewController
<UIImagePickerControllerDelegate,
UINavigationControllerDelegate>
#property BOOL newMedia;
#property (strong, nonatomic) IBOutlet UIImageView *imageView;
#property (strong, nonatomic) spViewController *secondViewController;
#property (strong, nonatomic) UIImageView *image;
#property (strong, nonatomic) UIImage *myImage;
- (IBAction)useCamera:(id)sender;
- (IBAction)useCameraRoll:(id)sender;
#end
spViewController.m
#import "spViewController.h"
#import "secondViewController.h"
#interface spViewController ()
#end
#implementation spViewController
- (IBAction)backtohome:(UIStoryboardSegue *)unwindSegue
{
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)useCamera:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = #[(NSString *) kUTTypeImage];
imagePicker.allowsEditing = NO;
[self presentViewController:imagePicker
animated:YES completion:nil];
_newMedia = YES;
}
}
- (IBAction)useCameraRoll:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = #[(NSString *) kUTTypeImage];
imagePicker.allowsEditing = NO;
[self presentViewController:imagePicker
animated:YES completion:nil];
_newMedia = NO;
}
}
//image picker delegate
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage]; self.myImage = chosenImage;
NSString *mediaType = info[UIImagePickerControllerMediaType];
[self performSelector:#selector(myMethod:) withObject:info afterDelay:0.1];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = info[UIImagePickerControllerOriginalImage];
_imageView.image = image;
if (_newMedia)
UIImageWriteToSavedPhotosAlbum(image,
self,
#selector(image:finishedSavingWithError:contextInfo:),
nil);
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
}
}
-(void)myMethod:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:^{
NSLog(#"Perform segue");
[self performSegueWithIdentifier:#"Picture Unwind Segue" sender:self];
}];
}
-(void)image:(UIImage *)image
finishedSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo
{
if (error) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: #"Save failed"
message: #"Failed to save image"
delegate: nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
//cancel delegate
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier]isEqualToString:#"Picture Unwind Segue"]) {
secondViewController *destinationViewController = [segue destinationViewController];
destinationViewController.imageView.image = self.myImage;
}
}
#end
secondViewController.h
#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import "spViewController.h"
#interface secondViewController : UIViewController
<UIImagePickerControllerDelegate,
UINavigationControllerDelegate>
#property (strong, nonatomic) IBOutlet UIImageView *imageView;
#property (strong, nonatomic) UIImage *myImage;
#property (strong, nonatomic) UIImageView *image;
#end
secondViewController.m
#import "secondViewController.h"
#import "spViewController.h"
#interface secondViewController ()
#end
#implementation secondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
self.imageView.image = self.myImage;
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
So the problem I'm having is, I can't get the image that has just been selected in the UIImagePickerController to be displayed in the UIImageView, my ImagePickerController was initiated by a button in my previous view controller "spViewController" (rootViewController) I am using Storyboards, I thought if I put in the UIImageView then connected it using an outlet reference it would display there, this doesn't seem to be the case though. If I put the UIImageView in the first view controller and do the same it will display the image but thats not where I want it to go.
Thank you for any help
To fix my problem i decided to save the image and call on it later heres the code i put into my secondViewController.m
{NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filepathJPG = [docsPath stringByAppendingPathComponent:#"imagefile.jpg"];
UIImage *img = [UIImage imageWithContentsOfFile: filepathJPG];
if (img != nil) {
// assign the image to the imageview,
_imageView.image = img;
// Optionally adjust the size
BOOL adjustToSmallSize = YES;
CGRect smallSize = (CGRect){0,0,100,100};
if (adjustToSmallSize) {
_imageView.bounds = smallSize;
}
}
I hope this can help others that have the same problem.
So according to your code , you are setting the property of myImage which is an instance of UIImage from the RootViewController.
so your just passing the data of the image , But your not setting your imageView.image property to the passed image value.
So a possible fix would be to set
imageView.image = myImage in viewDidLoad
or
you can directly access the imageView property from the perform prepare for segue method in the RootViewController and set its image property.
I hope this helps .
Cheers
You can declare a property in AppDelegate class and access it across all controllers using appdelegate shared instance

no visible #interface does not debug

-(IBAction)TakePhoto {
picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentedViewController:picker animated:YES completion:nil];
}
- (IBAction)ChooseExisting {
picker2 = [[UIImagePickerController alloc]init];
picker2.delegate = self;
[picker2 setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentedViewController:picker2 animated:YES completion:nil];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageview setImage:image];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:NULL];
When I try to run this app I the
[self presentedViewController:picker animated:YES completion:nil];
Gets highlighted in red saying No visible #interface for ViewController Declares the sector presentedviewcontroller:animated :completion :
My .h file is
#interface ViewController : UIViewController <UIImagePickerControllerDelegate,
UINavigationControllerDelegate> {
UIImagePickerController *picker;
UIImagePickerController *picker2;
UIImage *image;
IBOutlet UIImageView *imageview;
}
- (IBAction)TakePhoto;
- (IBAction)ChooseExisting;
You have a typo: presentedViewController instead of presentViewController on the line
[self presentedViewController:picker animated:YES completion:nil];
(in more than one place)

How to pass image, from uiimagepickercontroller, into another scene's uiimageview

How do I save a UIImagePickerController camera image so I can send to CreateViewController scene's UIImageView?
HomeViewController(scene 1) has a button that loads UIImagePickerController and returns with an image from the camera. CreateViewController(scene 2) has an empty UIImageView.
HomeViewController.h
#import "CreateViewController.h"
#interface HomeViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
#property (strong, nonatomic) UIImagePickerController *imagePicker;
#property(nonatomic, retain) UIImage *myImage;
- (IBAction)cameraImage:(id)sender;
HomeViewController.m
#implementation HomeViewController
#synthesize imagePicker, myImage;
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ( [segue.identifier isEqualToString:#"create"]) {
CreateViewController *cvc = [segue destinationViewController];
UIImage *image = myImage;
cvc.myImage = image;
}
}
//Camera button action
- (IBAction)cameraImage:(id)sender{
//UIImagePickerController space in memory
imagePicker = [[UIImagePickerController alloc]init];
//Set the delegate
imagePicker.delegate = self;
//Set the sourceType
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
//Show Image Picker UI
[self presentViewController:imagePicker animated:YES completion:^{}];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info {
self.myImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:YES completion:^{}];
[self performSegueWithIdentifier:#"create" sender:self];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:^{}];
}
CreateViewController.h
#property (strong, nonatomic) IBOutlet UIImageView *bgImage;
#property(nonatomic, retain) UIImage *myImage;
CreateViewController.m
#implementation CreateViewController
#synthesize bgImage, myImage;
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = myImage;
[bgImage setImage:image];
}
Download sample code here; http://code-blind.com/ios6-camera-picture-to-another-scenes-uiimageview/
Using a segue is not mandatory if you are instanciating the controller programmatically.
The easiest way to achieve that is something like
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage * pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
DisplayViewController * controller = [DisplayViewController new];
controller.imageView.image = pickedImage;
[self.navigationController pushViewController:controller animated:YES];
}
If you want to use a segue you need to manually invoke it by doing
[self performSegueWithIdentifier:#"your-segue-name" sender:self];
then you can use an ivar in your HomeViewController to store the pickedImage and pass it to your destinationViewController.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
DisplayViewController * controller = (DisplayViewController *)segue.destinationViewController;
controller.imageView.image = _pickedImage;
}
where _pickedImage is the ivar where you stored the image after it has been picked.
On ur first view
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
CreateViewController* sI = [self.storyboard instantiateViewControllerWithIdentifier:#"XXXXX"];
sI.myImage = image;
}
XXXXX is identifier of view.
First you have to be sure that your delegate method is called and that you have the image.
Next, store the image to a property:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
self.takenImage = image; // assuming you have declared #property(nonatomic, strong) UIImage *takenImage;
[picker dismissViewControllerAnimated:YES completion:^{
[self performSegueWithIdentifier:#"my-segue" sender:self];
}];
}
After that, this method's being called:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// you need to set the picked image to the controller:
DisplayViewController *controller; // get the controller
controller.image = self.takenImage;
}
in this code:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
if( i ==1)
{
[self dismissViewControllerAnimated:YES completion:^
{
Second_ViewController*secondview=[[UIStoryboard storyboardWithName:#"Main" bundle:nil]instantiateViewControllerWithIdentifier:#"Second_ViewController"];
secondview.bb_image=image;
[[NSUserDefaults standardUserDefaults ]setInteger:0 forKey:#"123"];
[[self navigationController] pushViewController:secondview animated:YES];
}];
}

Resources