i have an UIButton with Custom style and a default image. When i press it, i load the camera and when the photo is taken i want to change the image in the UIButton but it doesn’t work.
I’m using this code:
- (void) imagePickerController: (UIImagePickerController *) picker
didFinishPickingMediaWithInfo: (NSDictionary *) info {
UIImage *originalImage, *imageToSave;
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage];
imageToSave = originalImage;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
});
[self.imageButton setImage:originalImage forState:UIControlStateNormal];
hasPhoto = YES;
[self dismissModalViewControllerAnimated: YES];
}
Any idea? Thanks!
Have you checked that self.imageButton is not nil?
You may have simply failed to bind your self.imageButton outlet to the button in your nib (it’s easily done).
Or as Basically Bits suggests, the view (and any subviews including your button) may have been released when the image picker was shown, so you may need to force you view to reload before attempting to manipulate the button image. If that’s the case, call this before setting your image to re-load the button:
[self view];
This may appear like it would do nothing, but the view property is a lazy constructor, so calling it will load the view from the nib again if it was unloaded.