Using a UICollectionViewCell as an Button - ios

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

Related

how to add the camera capture images in array in 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;
}

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

MWPhotoBrowser will not display photo

I'm trying to implement MWPhotoBrowser in my project. I've added all the delegates, but it still will not display the photo.
Here's my didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"MWPhotoBrowserSegue" sender:self]
}
This is my segue method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue identifier] isEqualToString:#"MWPhotoBorwserSegue"])
{
photoBrowser = [[MWPhotoBrowser alloc]initWithDelegate:self];
UINavigationController * nc = [[UINavigationController alloc]initWithRootViewController:photoBrowser];
nc.modalTransitionStyle = UIModalTransitionStyleCrossDisolve;
[photos addObject:[MWPhoto photoWithImage:[UIImage imageNamed:#"picasa.jpg"]];
[photoBrowser setCurrentPhotoIndex:0];
[self.navigationController presentViewController:nc animated:YES completion:nil];
}
}
The #ofPhotosInBrowser:
-(NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser
{
return self.photos.count;
}
photoAtIndex:
- (MWPhoto *)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
if (index < self.photos.count)
return [self.photos objectAtIndex:index];
return nil;
}
Here's what I get after the didSelectRowAtIndexPath: is called:
Edit for Solution:
MWPhotoBrowser will not display a photo unless you put self.title in the initWithStyle: method. I also forgot to add the viewWillDisappear and viewWillAppear
I think u need to call reload data on it. Check Mwphotobrowser.h for proper way to do it.
try this [[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self presentViewController:yourNewNavigationController animated:YES completion:nil];
}];

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)

Resources