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;
...............................
Related
Here is my Code.Am very new to Xcode.When i tap on tableview cell am getting UIActionSheet.To dismiss UIActionSheet i used UITapGestureRecognizer.It's not Working for me.
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [aTableView cellForRowAtIndexPath:indexPath];
UILabel *lbl = (UILabel *)[cell.contentView viewWithTag:2];
NSLog(#"cell text label =%#",lbl.text);
_StoreNameobj = lbl.text;
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Store Maintenance Action"
delegate:self
cancelButtonTitle:cancelTitle
destructiveButtonTitle:nil
otherButtonTitles:#"Show Store Details",#"Navigate to Store",#"Edit Store Nickname",#"Delete Store", nil];
actionSheet.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOut:)];
[lbl addGestureRecognizer:tapGesture];
[cell.contentView addSubview:lbl];
}
- (void)tapOut{
//dismiss the action sheet here
[actionSheet removeFromSuperview];
}
Any Help is Appreciable.Thanks in Advance!
I think you not need to manually remove the actionsheet. By default actionsheet resigns on click. there is no need to take tab gesture i think.
Show actionsheet like,
[actionSheet showInView:self.view];
so when you clik on any button it will dissmiss automatically
To view UIActionSheet on UITableViewCell apply this code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:#"Your Title:" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:
#"button title1", nil];
[popup show];
}
add TapGesture
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UITapGestureRecognizer *tapp = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(HideActionSheet:)];
[tapp setNumberOfTapsRequired:1];
tapp.cancelsTouchesInView = NO;
[self.view.window tapp];
[tapp release];
}
HideActionSheet code:
- (void) HideActionSheet:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
CGPoint location = [sender locationInView:nil];
if (![self.view pointInside:[self.view convertPoint:location fromView:self.view.window] withEvent:nil])
{
[self.view.window removeGestureRecognizer:sender];
[self dismissModalViewControllerAnimated:YES];
}
}
}
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];
}
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;
}
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];
}];
}
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.