0

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];
}
kos
  • 1,357
  • 9
  • 21
  • Another piece of information, it is setting the background, but seems that it is not redrawing it. the button is in the custom cell in the table, I did try to do [table reloadData].. seems like I need some kind of redraw/refresh on the button? and setNeedsDisplay does not work. – kos Sep 14 '13 at 21:14
  • Definitely a paint redraw issue, scrolling table repaints the image correctly. But how do I solve this? I am doing all this while in table edit mode. – kos Sep 14 '13 at 22:43
  • Can you post the code that is creating the cell? Does it fix itself when scrolling the cell offscreen? – nrj Sep 14 '13 at 23:02

2 Answers2

0

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];
}
Furkan Mustafa
  • 794
  • 7
  • 12
  • Yeah good call only it does not matter images are not edited. But thanks anyway. I guess I left some code trying different things. No this does not work. – kos Sep 14 '13 at 22:30
0

This (Is it possible to refresh a single UITableViewCell in a UITableView?) solves the problem! Redrawing sell as recommended in the link works.

Community
  • 1
  • 1
kos
  • 1,357
  • 9
  • 21