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.
Related
We have to let the user open the photo album, pick an image, and edit it .
So using the picker, i can see all the user albums, than when enter an album i can see the images, than, when tapping an image, i get the delegate .
Why i don't see the check sign when choosing an image ? its unclear that you actually pick it. can you also pick more than one, or is it still disabled ?
I can't edit the selected image, after i pick it, i got the delegate called, but i don't have the editing button anywhere .
UIImagePickerController *pickerLibrary = [[UIImagePickerController alloc] init];
pickerLibrary.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
pickerLibrary.delegate = self;
pickerLibrary.editing=YES;
[self presentViewController:pickerLibrary animated:YES completion:nil];
-(void)imagePickerController:
(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
{
UIImage *image = info[UIImagePickerControllerOriginalImage];
UIImage *imageE = info[UIImagePickerControllerEditedImage];
NSLog(#"%#",image);
NSLog(#"%#",imageE);
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
// Media is a video
}
// Code here to work with media
[self dismissViewControllerAnimated:YES completion:nil];
}
Ok , answer is this :
pickerLibrary.allowsEditing=YES;
and not isEditing
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 am trying to set a UIButton image with the image chosen from an image picker. My code looks like this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
// Resize image
UIGraphicsBeginImageContext(CGSizeMake(478, 640));
[editedImage drawInRect: CGRectMake(0, 0, 478, 640)];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.cameraButton setImage:smallImage forState:UIControlStateNormal];
}
When this code executes it removes the original image I had set and shows nothing. If I click the space where the image button was, it still kicks off the IBAction, but no image.
I have connected the IBOoutlet so it is not that.
What am I doing wrong?
whoops, UIImagePickerControllerEditedImage should be UIImagePickerControllerOriginalImage
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"];
}
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.