how to add the camera capture images in array in ios - ios

Want to add the camera capture images added in array and load that array in collection view display the all the images
///here i get the capture image in picker
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:nil];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// in this part how can add the camera capture image in array
// and load that array value in collection view..
}
help me how to achieve this...

I have not tested what I am about to write, but you could try this:
-(void)viewDidLoad {
//declare before NSMutableArray *_mutableArray;
_mutableArray = [[NSMutableArray alloc] init];
}
...
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//dismiss UIImagePickerController
[picker dismissViewControllerAnimated:YES completion:Nil];
//take image from info
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//store image into NSMutableArray
[_mutableArray addObject:image];
//reload collectionView
[self.collectionView reloadData];
}
and in collectionView cellForItemAtIndexPath
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//this is a custom CollectionViewCell
CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
cell.imageA.image = [_mutableArray objectAtIndex:indexPath.row];
return cell;
}

Related

Using a UICollectionViewCell as an Button

how do I set a UICollectionViewCell to be used as a button , that also repeats itself when the function is done.
I want to use the cell as a button to add images from my photo library.
The code extract is the function of the barbutton item I used to add images.
- (IBAction)buttonTapped:(id)sender {
_picker = [[UIImagePickerController alloc] init];
_picker.delegate = self;
_picker.allowsEditing = NO;
_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:_picker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
[_images addObject:image];
[self saveNewImageToDb:image];
[self dismissViewControllerAnimated:_picker completion:^{
[_collectionView reloadData];
}];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:_picker completion:nil];
}
You can implement this function :
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// something you do as function "buttonTapped"
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Selected cell = %d",indexPath.item);
_picker = [[UIImagePickerController alloc] init];
_picker.delegate = self;
_picker.allowsEditing = NO;
_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:_picker animated:YES completion:nil];
}

Duplicate Declaration of method' startCameraControllerfromViewController:usingDelegate:'

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

Added photo does not fit in the frame of UIImageView

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;
...............................

How to display an image that is selected in one view in another view

I used the code below in iOS to pick an image from the photo library. I need to display this image in different view. How should I proceed?
//delegate methode will be called after picking photo either from camera or library
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:NO completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSArray *theViewControllers = [self.tabBarController viewControllers];
[imgview setImage:image]; //set in image view
[self performSegueWithIdentifier:#"NextView" sender:nil];
}
I tried the below code to display the image in next view but it is not working??
//delegate methode will be called after picking photo either from camera or library
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:NO completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSArray *theViewControllers = [self.tabBarController viewControllers];
//[imgview setImage:image]; //set in image view
ImageViewController *adjViewController = (ImageViewController *)[theViewControllers objectAtIndex:0];
[adjViewController.imageview1 setImage :image];
[self performSegueWithIdentifier:#"NextView" sender:nil];
}
You could make the UIImage *image (from your imagePickerController method) global to its view controller and then pass it prepareForSegue.
For example:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"NextView"]) {
ImageViewController *vc = [segue destinationViewController];
[vc setImage:image];
}
}
Then in your ImageViewController.h you just need something like:
#interface ImageViewController : UIViewController {
UIImage *image;
}
#property (nonatomic, retain) UIImage *image;
And synthesise it in the .m and set the image to your imageView in viewWillAppear. You can read the docs for prepareForSegue here.

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