I have two images in one view controller which have been picked with UIImagePickerController:
ChoosePhotosViewController.h
#interface PickPhotosViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
#property (strong, nonatomic) IBOutlet UIImageView *pickedImage1; // image 1
#property (strong, nonatomic) IBOutlet UIImag8eView *pickedImage2; // image 2
- (IBAction)pickImage1:(id)sender; // button that picks up the first image
- (IBAction)pickImage2:(id)sender; // button that picks up the seconds image
- (IBAction)nextButton:(id)sender;
in implementation file - ChoosePhotosViewController.m
#implementation PickPhotosViewController
#synthesize pickedImage1, pickedImage2;
for each button I wrote a code which invokes action sheet with a list of actions:
-(IBAction)pickImage1:(id)sender {
[[NSUserDefaults standardUserDefaults] setInteger:CurrentImageCategoryImage1 forKey:#"currentImageCategory"];
[[NSUserDefaults standardUserDefaults] synchronize];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Choose image one" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Choose existing photo", #"Take new photo", nil];
[actionSheet showInView:self.view];
}
There are two actions - take photo from camera air upload from library:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
else if (buttonIndex == 1) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
}
The similar algorithm for the second button. Then in UIImagePickerController method I wrote;
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
if([[NSUserDefaults standardUserDefaults] integerForKey:#"currentImageCategory"] ==CurrentImageCategoryImage1)
{
self.pickedImage1.image = chosenImage;
}
else if([[NSUserDefaults standardUserDefaults] integerForKey:#"currentImageCategory"] ==CurrentImageCategoryImage2)
{
self.pickedImage2.image = chosenImage;
}
}
Then I need to display these two images in the next viewController when I click button “next”. I work with storyboards, that is why I wrote prepareForSegue method:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#“next"]) {
TypeTextViewController *transferedImage = [segue destinationViewController];
transferedImage.imageWithText1 = pickedImage1;
}
}
where imageWithText1 is a UIImageView and declared in TypeTextViewController.h, but When I click button images don’t appear in the TypeTextViewController.
Where is a problem?
You want to create NSMutableArray and then this two Images add into this Array.And then you have to pass this NSMutableArray to Another View Controller.
Hope this is helpfull.
I dont think you can pass a UIImageView from one VC to another VC. I think we can only pass UIImage and not UIImageView. Try this.. But im not sure.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#“next"])
{
TypeTextViewController *transferedImage = [segue destinationViewController];
transferedImage.imageWithText1.image = pickedImage1.image;
}
}
// pass Image one view to other view:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
[self dismissViewControllerAnimated:YES completion:^
{
color_ViewController*secondview=[[UIStoryboard storyboardWithName:#"Main" bundle:nil]instantiateViewControllerWithIdentifier:#"color_ViewController"];
secondview.b_image=image;
[[NSUserDefaults standardUserDefaults ]setInteger:1 forKey:#"123"];
[[self navigationController] pushViewController:secondview animated:YES];
}];
}
Related
I have two UI Buttons in my view controller, and an imageview. One button has the user pick a picture from the photo library and add it to the UIImage. The other has the user take a new picture and show it in the UIimage.
The libraryButton and actions all work, however when I add the cameraButton code I get an error since I'm using the same "startcameracontrollerfromviewcontroller" My question is can I simply change this to startcameracontrolfromviewcontroller? or is there something else I would have to change.
#pragma mark - Button Actions
- (IBAction)libraryButton:(id)sender {
[self startCameraControllerFromViewController:self usingDelegate:self];
}
#pragma mark - UIImagePickerControllerDelegate methods
- (BOOL) startCameraControllerFromViewController: (UIViewController*) controller usingDelegate: (id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>) delegate {
if (([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera] == NO) || (delegate == nil) || (controller == nil))
return NO;
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
cameraUI.delegate = delegate;
[controller presentViewController:cameraUI animated:YES completion:nil];
return YES;
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage * image = info[UIImagePickerControllerOriginalImage];
_imageView.image = image;
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - Camera Button
- (IBAction)cameraButton:(id)sender {
[self startCameraControllerFromViewController:self usingDelegate:self];
}
#pragma mark - Camera Delegate methods
- (BOOL) startCameraControllerFromViewController: (UIViewController*) controller usingDelegate: (id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>) delegate {
if (([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera] == NO) || (delegate == nil) || (controller == nil))
return NO;
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.delegate = delegate;
[controller presentViewController:cameraUI animated:YES completion:nil];
return YES;
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage * image = info[UIImagePickerControllerOriginalImage];
_imageView.image = image;
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
You can call the same method from each IBAction passing it a different value that will be used to determine the image pickers source type - (for this example I'm not passing in the presenting view controller or delegate, but this could be easily adapted to implement)
- (IBAction)libraryButton:(id)sender {
[self showImagePickerForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
- (IBAction)cameraButton:(id)sender {
[self showImagePickerForSourceType:UIImagePickerControllerSourceTypeCamera];
}
- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType {
// start config here....
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
// set the source type here
imagePickerController.sourceType = sourceType;
// do any extra setting up... present controller...
}
I suggest looking at this sample code provided by Apple - Using UIImagePickerController to Select Pictures and Take Photos
please help to push added photo in the frame of UIImageView. The photo doesn't fit in the frame. I have made all the same like in the "IOS Programming: The Big Nerd Ranch Guide" chapter 12 "Camera". Before adding photo: https://www.dropbox.com/s/o0uypvspuhvlkm2/Screen%20Shot%202013-10-27%20at%205.08.39%20PM.jpg After adding: https://www.dropbox.com/s/u6qffb1l941cb0v/Screen%20Shot%202013-10-27%20at%205.09.02%20PM.jpg I've tried setContentMode for UIImageView – doesn't works. This is my code:
#pragma mark UIActionSheetDelegate
- (IBAction)displayActionSheet:(id)sender
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Take photo", #"Choose photo from gallery", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
[self takePhoto];
break;
case 1:
[self choosePhoto];
break;
}
}
#pragma mark UIImagePickerControllerDelegate
- (void)takePhoto {
UIImagePickerController *pickerControllerTakePhoto = [[UIImagePickerController alloc] init];
[pickerControllerTakePhoto setSourceType:UIImagePickerControllerSourceTypeCamera];
[pickerControllerTakePhoto setDelegate:self];
[self presentViewController:pickerControllerTakePhoto animated:YES completion:nil];
}
- (void)choosePhoto {
UIImagePickerController *pickerControllerChoosePhoto = [[UIImagePickerController alloc] init];
[pickerControllerChoosePhoto setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[pickerControllerChoosePhoto setDelegate:self];
[self presentViewController:pickerControllerChoosePhoto animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *myPhoto = [info objectForKey:UIImagePickerControllerOriginalImage];
[photoView setImage:myPhoto];
[self dismissViewControllerAnimated:YES completion:NULL];
}
My 'plus' or 'add' button calls action sheet with dialog, so I choose the photo from gallery and result is like on second screenshot.
Set the image views properties:
imageView.clipsToBounds = YES;
imageView.contentMode = UIViewContentModeScaleAspectFill;
Both can be set in Interface Builder.
So I've found the line of code that disturbed me, this all because of my custom cells which I put in order to my tableview:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
if (indexPath.row == 0) {
photoView = (UIImageView *)[photoCell viewWithTag:1]; // this is the first custom cell in my tableview and this line which I put on the model of the book isn't needed
cell = photoCell;
...............................
-(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)
I have a storyboard app which has a UIViewController and a UICollectionViewController. In the view controller, the user chooses multiple photos from the iPhone's photo library (Since there is no API for multi-select in iOS, I used ELCImagePickerController to achieve this). And it segues to the collection view controller where the selected photos should be shown in little image views.
The image library shows up and I am able to select multiple photos. But when it segues to the collection view controller, it throws the -[UIImage length]: unrecognized selector sent to instance error in the collection view's cellForItemAtIndexPath event.
Below is the code I have so far.
ViewController.h
#import <UIKit/UIKit.h>
#import "ELCImagePickerController.h"
#import "ELCAlbumPickerController.h"
#import "ELCAssetTablePicker.h"
#import "GalleryViewController.h"
#interface ViewController : UIViewController
#property (strong, nonatomic) NSMutableArray *cameraImages;
- (IBAction)chooseImages:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (IBAction)chooseImages:(id)sender
{
UIActionSheet *photoSourcePicker = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Take Photo", #"Choose from Library", nil, nil];
[photoSourcePicker showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
ELCAlbumPickerController *albumController = [[ELCAlbumPickerController alloc] initWithNibName:nil bundle:nil];
ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initWithRootViewController:albumController];
albumController.parent = elcPicker;
elcPicker.delegate = self;
if ([self.view respondsToSelector:#selector(presentViewController:animated:completion:)]){
[self presentViewController:elcPicker animated:YES completion:nil];
} else {
[self presentViewController:elcPicker animated:YES completion:nil];
}
}
else {
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"This device doesn't have a camera"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[alert show];
}
break;
case 1:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
ELCAlbumPickerController *albumController = [[ELCAlbumPickerController alloc] initWithNibName:nil bundle:nil];
ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initWithRootViewController:albumController];
albumController.parent = elcPicker;
elcPicker.delegate = self;
if ([self.view respondsToSelector:#selector(presentViewController:animated:completion:)]){
[self presentViewController:elcPicker animated:YES completion:nil];
} else {
[self presentViewController:elcPicker animated:YES completion:nil];
}
}
else {
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"This device doesn't support photo libraries"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[alert show];
}
break;
}
}
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
self.cameraImages = [[NSMutableArray alloc] initWithCapacity:info.count];
for (NSDictionary *camImage in info) {
UIImage *image = [camImage objectForKey:UIImagePickerControllerOriginalImage];
[self.cameraImages addObject:image];
}
/*
for (UIImage *image in info) {
[self.attachImages addObject:image];
}
*/
NSLog(#"number of images = %d", self.cameraImages.count);
if (self.cameraImages.count > 0) {
[self performSegueWithIdentifier:#"toGallery" sender:nil];
}
}
- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker
{
if ([self respondsToSelector:#selector(dismissViewControllerAnimated:completion:)]) {
[self dismissViewControllerAnimated:YES completion:nil];
} else {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"toGallery"]) {
GalleryViewController *galleryVC = [segue destinationViewController];
galleryVC.selectedImages = self.cameraImages;
}
}
#end
GalleryViewController.h
#import <UIKit/UIKit.h>
#import "ELCImagePickerController.h"
#import "ELCAlbumPickerController.h"
#import "ELCAssetTablePicker.h"
#import "ImageCell.h"
#interface GalleryViewController : UICollectionViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
#property (strong, nonatomic) NSMutableArray *selectedImages;
#end
GalleryViewController.m
#import "GalleryViewController.h"
#interface GalleryViewController ()
#end
#implementation GalleryViewController
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.selectedImages.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"imgCell" forIndexPath:indexPath];
UIImage *image;
int row = indexPath.row;
image = [UIImage imageNamed:self.selectedImages[row]]; //This is where it throws the error
cell.imageView.image = image;
return cell;
}
#end
To further demonstrate the issue, I've slapped together a demo project which you can download from here.
I know thus question has been asked many time before here on SO. I tried them all but to no avail prior to posting my question here.
I'd appreciate if someone can tell me how to get rid of this error.
Thank you.
Hey I understood your problem you are already having an array of images, why using imageNamed: constructor again.
image = [UIImage imageNamed:self.selectedImages[row]];
cell.imageView.image = image;
//This throws a exception because, you have UIImage objects in your array and here imageNamed: takes NSString as an argument , so you are trying to pass a UIImage object instead of a NSString object
Directly take out image from array and assign like this:
cell.imageView.image = (UIImage*) [self.selectedImages objectAtIndex:row];
UIImage * is probably not needed.
self.selectedImages[row] should be a NSString. It seems like it is a UIImage instead of an NSString. Its trying to call length method on the UIImage instance.
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];
}];
}