This code i use for the purpose
if(editedImage !=nil)
{
[selectionView1 removeFromSuperview];
UIImageView *selectionView1 = [[UIImageView alloc] initWithImage:editedImage.image];
[_buttonScroll addSubview:selectionView1];
}
else{
[_buttonScroll addSubview:selectionView1];
}
i did in way when first time it executes then editedImage is nil so it goes in else then later on editedImage gets images like below
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage : (UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
NSLog(#"%#",image);
editedImage = [[UIImageView alloc] initWithImage: image];
NSLog(#"%#",editedImage);
// editedImage is UIImageView
[picker dismissModalViewControllerAnimated:YES];
}
so it get filled and when second time my first code execute then editedImage is not null and it goes inside of if(editedImage !=nil) condition and for that now i want to delete old image which was on button and add this new image which i got from edited image
so the problem is old image is not getting removed what am i doing wrong can you please suggest me something
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage : (UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
[_buttonScroll setImage:image forState:UIControlStateNormal];
[picker dismissModalViewControllerAnimated:YES];
}
try this simply :)
Try:
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage : (UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
NSLog(#"%#",image);
if(editedImage)
[editedImage removeFromSuperview];
editedImage = [[UIImageView alloc] initWithImage: image];
NSLog(#"%#",editedImage);
// editedImage is UIImageView
[picker dismissModalViewControllerAnimated:YES];
}
What you need to do is keep track of the image that you are adding. You can either keep a property variable to the image that is being added, but a better option is to just set the tag of the image that gets added. If you change your first if statement to as follows:
//set a tag value to use (defined at the top of your file outside the #implementation)
#define PREVIOUS_VIEW_TAG 999
if(editedImage !=nil)
{
UIView* previousImageView = [_buttonScroll viewWithTag:PREVIOUS_VIEW_TAG];
[previousImageView removeFromSuperview];
UIImageView *selectionView1 = [[UIImageView alloc] initWithImage:editedImage.image];
selectionView1.tag = PREVIOUS_VIEW_TAG;
[_buttonScroll addSubview:selectionView1];
}
else{
selectionView1.tag = PREVIOUS_VIEW_TAG;
[_buttonScroll addSubview:selectionView1];
}
This should let you keep track of the view that you are adding. The value of the PREVIOUS_VIEW_TAG could be anything, I just picked 999.
Also keeping in mind what #Bhargavi suggested, using [_buttonScroll setBackgroundImage:selectionView1] and then removing with [_buttonScroll setBackgroundImage:nil]; (or just override by setting the new backgroundImage) would also work. However, to specifically answer your question, perhaps consider keeping track of the view with a tag.
Related
I have a UImage view that opens and you can take a picture with it and view it in the uiimageview. But I added another image view and copied the code and now the image shows up the same image as the second one. I believe it may have something to do with the '[UIImagePickerControllerOriginalImage];'
- (void)imagePickerController:(UIImagePickerController *)
picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
// Get the image and store it in the image view
image = info[UIImagePickerControllerOriginalImage];
self.personimgThumbNail.image = image;
}
- (void)imagePickerControllertwo:(UIImagePickerController *)
picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
// Get the image and store it in the image view
imagetwo = info[UIImagePickerControllerOriginalImage];
self.personimgThumbNailtwo.image = imagetwo;
}
Just need a next step, been stuck on this one for quite a while.
There can be only one didFinish-Method. You have to differentiate between what to do inside the method itself. The method already gives you the UIImagePickerController, which is calling the method, so you just have to compare the pointers to it.
- (void)imagePickerController:(UIImagePickerController *)
picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
if(picker == self.pickerController1){
// Get the image and store it in the image view
image = info[UIImagePickerControllerOriginalImage];
self.personimgThumbNail.image = image;
}else if(picker == self.pickerController2){
// Get the image and store it in the image view
imagetwo = info[UIImagePickerControllerOriginalImage];
self.personimgThumbNailtwo.image = imagetwo;
}
}
Edit: You have to have 2 properties defined in the .m file of your class
#property (strong) UIImagePickerController *pickerController1;
#property (strong) UIImagePickerController *pickerController2;
What you have to do now, when instantiating your image-picker is the following
(code taken from the OPs comment under this answer)
- (IBAction)accessPhotoLibrary:(id)sender {
if(!self.pickerController1){
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
self.pickerController1 = imagePicker;
}
[self presentViewController:self.pickerController1 animated:YES completion:nil];
}
I am currently taking a photo using this code here
- (void) cameraButtonSelected
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
I am allowing the user to edit the the photo but when I use this delegate method for some reason the UIImagePickerController will not remove from view after the user has pressed "Use Photo"
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
I would like to know
How do I remove UIImagePickerController from view after "Use Photo" button pressed
How can I resize the photo I have just taken as I need a smaller variant to send to my server
You can get the image by querying for UIImagePickerControllerEditedImage in the info dict.
And to remove the ImagePicker from View just dismiss the picker.
Here is the code to resize.
Just call it with your image instance
You can use this function to scale the image to particular size
- (UIImage *) scaleImage:(UIImage*)image toSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
So your final code should be something like this
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:Nil];
UIImage *image = info[UIImagePickerControllerEditedImage];
image = [self scaleImage:image toSize:CGSizeMake(200,200)]; // or some other size
}
simple , you can search it here on stackoverflow
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *tempImage=[info objectForKey:UIImagePickerControllerEditedImage];
[self.dealImageView setImage:[self imageWithImage:tempImage convertToSize:CGSizeMake(200, 200)]];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (UIImage *)imageWithImage:(UIImage *)image convertToSize:(CGSize)size {
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return destImage;
}
whats-the-easiest-way-to-resize-optimize-an-image-size-with-the-iphone-sdk
resizing-and-cropping-a-uiimage
Call [self dismissModalViewControllerAnimated:YES completion:nil] somewhere in your - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info method. This is common practice for any modal view.
This question is probably more complicated than you might think. There are a number of ways to resize a photo in iOS and the code you use really will depend on your needs. However you can take a look at this blog post to get an understanding of photo resizing. It's very comprehensive and I would recommend reading it before you write any code.
http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
That being said this is a very simple way to achieve resizing:
The simplest way to resize an UIImage?
I have two UIImageView in the same view.for each imageView I should assign a picture taken from the camera or photos library.
When I start by doing that, I get the same photo for the two imageView.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
imageView2.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
-(IBAction)addPhoto:(id)sender{
UIActionSheet *actionSheet =[[UIActionSheet alloc]initWithTitle:#"" delegate:self cancelButtonTitle:#"cancel" destructiveButtonTitle:#"Choose Photo" otherButtonTitles:#"Take Photo ", nil];
[actionSheet showInView:self.view];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
shouldUpdateFirstImage =YES;
// chosenImage = info[UIImagePickerControllerEditedImage];
[picker dismissViewControllerAnimated:YES completion:nil];
if (shouldUpdateFirstImage) {
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
shouldUpdateFirstImage = NO;
}
else {
pictureView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
shouldUpdateFirstImage = YES;
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Put Picture"])
{
alrtView.hidden = YES;
pictureView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,100,100)];
[self.view addSubview:pictureView];
[self addPhoto:self];
}
And the viewController.h
#interface ViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate,UIActionSheetDelegate,UITextViewDelegate>
{
BOOL shouldUpdateFirstImage;
}
The first ImageView is the background.Through an AlertView I choose to add Picture to the first ImageView.After that I recall the same actionsheet to choose the taken photo.
TL;DR you need to track the state of your class, and have your delegate method respond accordingly to that state.
It sounds like you need your class to keep track of how many images have been displayed, and load new selections based on that information. We call this "state", and you have to implement the logic for your imagePickerController delegate method to respond appropriately to that state.
One possible approach would be to have a BOOL scoped to your class (either an ivar or property will do, but ivar is likely more appropriate), then use it's value to keep track of which imageView should be updated.
Assuming you already declared a BOOL named shouldUpdateFirstImage, you could do something like this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
if (shouldUpdateFirstImage) {
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
shouldUpdateFirstImage = NO;
}
else {
imageView2.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
shouldUpdateFirstImage = YES;
}
}
This would alternate which imageView was updated with each successive selection.
Try this
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:nil];
if(imageView.image)
imageView2.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
else
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
I been stuck on this for a while since it is so weird. I present a user with image picker and on selection use the image as a button background. The problem is that it works only after image is picked for the 2nd time. Do not have to be the same image. Present image picker select picture, nothing. Try again for the same or different picture and everything works just fine. Code is below, does anyone have any suggestions?
-(void) changeProfileImage:(UIImage*) image {
profilePhoto = image;
[editPhotoButton setTitle:#"edit" forState:UIControlStateNormal];
[editPhotoButton setBackgroundImage:profilePhoto forState:UIControlStateNormal];
[editPhotoButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *) imageInfo {
UIImage* imageToUse;
UIImage* editedTmpImage = [imageInfo objectForKey:#"UIImagePickerControllerEditedImage"];
if (!editedTmpImage)
imageToUse = [imageInfo objectForKey:#"UIImagePickerControllerOriginalImage"];
imageToUse = [imageToUse resizedImageWithContentMode:UIViewContentModeScaleAspectFill bounds:CGSizeMake(MIN(imageToUse.size.width,1024), MIN(imageToUse.size.width, 768)) interpolationQuality:kCGInterpolationHigh];
[self changeProfileImage:imageToUse];
[imagePicker dismissViewControllerAnimated:YES completion:Nil];
}
First, just to make sure, can you just clean it up a little bit like this and try again?
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *) imageInfo {
UIImage* imageToUse = [imageInfo objectForKey:#"UIImagePickerControllerEditedImage"];
if (!imageToUse) { // this part was confusing above, please use curvy-braces
imageToUse = [imageInfo objectForKey:#"UIImagePickerControllerOriginalImage"];
}
imageToUse = [imageToUse resizedImageWithContentMode:UIViewContentModeScaleAspectFill bounds:CGSizeMake(MIN(imageToUse.size.width,1024), MIN(imageToUse.size.width, 768)) interpolationQuality:kCGInterpolationHigh];
[self changeProfileImage:imageToUse];
[imagePicker dismissViewControllerAnimated:YES completion:nil];
}
This (Is it possible to refresh a single UITableViewCell in a UITableView?) solves the problem! Redrawing sell as recommended in the link works.
I have 6 Imageviews set with tags. In one tap on the imageview, i call the UIActionSheet to show button "Take Pic" & "select Pic". All this works fine.
in the imagePickerController where i finally set the selected image how do i know which Imageview to update the pic.
Please let me know how i can pass my tag values to update the correct tapped Image.
here's my code.
viewDidLoad
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(tappedImage:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[self.photo1 addGestureRecognizer:tapGestureRecognizer];
[self.photo2 addGestureRecognizer:tapGestureRecognizer];
[self.photo3 addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer setDelegate:self];
tappedImage
- (void)tappedImage:(UIGestureRecognizer *)sender {
{
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:#"Add a Picture"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Take Picture",#"Select Picture", nil];
[actionSheet showInView:self.view];
}
}
imagePickerController
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
[picker dismissModalViewControllerAnimated:YES];
[photo1 setImage:image];
}
For now all the images are being set to Photo1 Imageview.
Any help will be greatly appreciated.
Add an instance variable to your class to store the selected image. Then in your tappedImage: method you do:
_selectedImage = (UIImageView *)sender.view;
where _selectedImage is your instance variable of type UIImageView *.
Then in the imagePickerController method, you can do:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
[picker dismissModalViewControllerAnimated:YES];
[_selectedImage setImage:image];
}