trying to set an image as button image after picking the image from uiimagepicker - objective-c

i have tried to get an answer to this question for a while. what i am trying to do is fire the UIImagePickerController and either choose from camera or select from goto library. this part works just fine. then i want to set that image as the button image on top of the button i am using to fire the image picker. in iOS 6 that was working just fine. since iOS 7 the image does not get set as the button image. i have tried many links and read many posts and went through even dev forum with no success in finding an answer. this is the last part of my desperate attempt to find an answer before going to apple technical support. i am hoping someone would be able to help me out. below is the code i am using to set the image to the button after selecting it from UIImagePickerController.
- (IBAction)choosePhoto
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:nil];
}
- (IBAction)takePhoto {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if (imagePicker) {
self.image = [info objectForKey:UIImagePickerControllerEditedImage];
[self.choosePhotoButton setImage:self.image forState:UIControlStateNormal];
} else {
if (picker) {
self.image = [info objectForKey:UIImagePickerControllerEditedImage];
[self.takePhotoButton setImage:self.image forState:UIControlStateNormal];
}
}
[self dismissViewControllerAnimated:YES completion:nil];
imagePicker = nil;
}
currently after selecting either button the image set on it is the tint color of the button as it shows in the screen shot below. the two white squares are the buttons where the picked image should be.
can someone take a look at my code and help me solve this issue?

You should look at the document called "Buttons" that's referenced in the UIButton Class Reference. When you add an image to a button (System type), it's added to the left of the title and as a template image, unless you set the rendering mode of the image to UIImageRenderingModeAlwaysOriginal. So, if you want the image to the left, you can do this:
- (IBAction)setImageForSender:(id)sender {
UIImage *buttonImage = [UIImage imageNamed:#"pic2.jpg"];
buttonImage = [buttonImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[sender setImage:buttonImage forState:UIControlStateNormal];
}
If you want the image under the title (and centered in the button) set the background image instead. This shows up normally, you don't have to deal with the rendering mode.
- (IBAction)setImageForSender:(id)sender {
UIImage *buttonImage = [UIImage imageNamed:#"pic2.jpg"];
[sender setBackgroundImage:buttonImage forState:UIControlStateNormal];
}
Here is the relevant portion of that "Buttons" document:
Images
Using the Image (currentImage) field, you can specify an image to
appear within the content of your button. If the button has a title,
this image appears to the left of it, and centered otherwise. The
image does not stretch or condense, so make sure to select an image
that is the proper size to appear in your button. Note that this image
will be automatically rendered as a template image within the button,
unless you explicitly set its rendering mode to
UIImageRenderingModeAlwaysOriginal. For more information, see Template
Images.
The Background (currentBackgroundImage) field allows you to specify an
image to appear behind button content and fill the entire frame of the
button. The image you specify will stretch to fill the button if it is
too small. It will be cropped if it is too large.
That the last piece of information in the text above about the image being cropped if it's too large, only happens if you set a specific size for your button. If its size is determined by its intrinsic content size (that's the default if you just drag out a button in IB and don't give it size constraints), it will expand to the size of the image.

Related

I cannot edit picture using UIImagePickerController in Xcode6

I wrote a application that take pictures from PhotoLibray. When I run the app, it succeed, and then I select a picture from the library, and I set the allowEditing property to YES, but when the edit screen shows, no edit tool show, I cannot edit the picture. I used xcode6, the class is UIImagePickerController. Code is below,
- (IBAction)takePicture:(id)sender
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// If the device ahs a camera, take a picture, otherwise,
// just pick from the photo library
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
imagePicker.allowsEditing = YES;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
imagePicker.delegate = self;
// Place image picker on the screen
[self presentViewController:imagePicker animated:YES completion:nil];
}
When the edit screen shows, there are 2 buttons, one is "Cancel" and the other is "choose". I press the left mouse button and drag it, but nothing shows, I can only move the picture and when I release the left mouse, the picture return back to the old place.
Why I cannot edit or crop the picture?
See UIImagePickerController allowsEditing will only allow you to crop image. It does nothing more. If you want to add more editing features you should implement by your self.
I was wrong, I can edit it. I search this using Internet, I can move and scale the picture and choose the picture in the square area, then get the picture after edited.The problem was resolved.

Image as a title in NavigationItem

I have a tab controller with four items in it. Second item is a navigation controller with a table view. I want to place logo of my app instead of title. Finally I can show my logo, but I can't understand how to scale it down to the size of Navigation Bar. Size of my image is 59x68px.
I do the followings thing in MyTableViewController viewDidload
[super viewDidLoad];
self.navigationController.navigationBar.translucent= NO;
UIImage *image = [UIImage imageNamed:#"assorti_logo.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:image];
myImageView.contentMode = UIViewContentModeScaleAspectFill;
self.navigationItem.titleView = myImageView;
I tried a lot of solutions from here, and non works. Is there any way to scale this image to match navigation bar height. Or what size of image should I use to see correct image both on iPhone 4s and 5?
I've finally found out the problem. I needed only to rename my image file to "logo#2x.png" The key is "#2x" in the end.

UITabBar unselected icon tint

I am trying to change the color of my tab bar icons when the tabs are UNselected. Right now the color is default grey and I can change the color to whatever color I want for when it IS selected.
Apple's dev library said to change the image rendering to "original" instead of its default mode "template." I did that. then it says to use initWithTitle:image:selectedImage: I tried to do that as well but I think that's where I messed up. I wrote this in my viewcontroller.m file. What's wrong here?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *stat = [UIImage imageNamed:#"white_stats.png"];
stat = [stat imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
(instancetype)initWithTitle:(NSString *)nil image:(UIImage *)stat selectedImage:(UIImage *)stat;
}
The problem is that you are using the same UIImage with UIImageRenderingModeAlwaysOriginal in both places.
Your code should look something like
UIImage *stat = [UIImage imageNamed:#"white_stats.png"];
UIImage *statAlwaysOriginal = [stat imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBarItem = [[UITabBarItem alloc] initWithTitle:nil image:statAlwaysOriginal selectedImage:stat];
The other thing is that there are some actual syntax errors in your post (in the UITabBarItem initialization, but I suspect you just pasted it incorrectly.

UIButton with background image: title text randomly does not appear

I am setting the background image on a UIButton which has title text. This works fine, except that about 5% of the time, rarely and non-deterministically, with the same code, the text does not appear.
In my UIButton subclass:
[self setBackgroundImage:normalImage forState:UIControlStateNormal];
// Also set a background image for all other states.
In a UIButton category, I have this convenience method for setting the title:
- (void)setNormalTitle:(NSString *)title {
[self setTitle:title forState:UIControlStateNormal];
[self setTitle:title forState:UIControlStateSelected];
[self setTitle:title forState:UIControlStateHighlighted];
[self setTitle:title forState:UIControlStateSelected | UIControlStateHighlighted];
}
I'm not finding this issue having been encountered by anyone. Any ideas? I can always create a custom UIView which contains the button and draws text on top using a UILabel, but I wonder if there's a known issue I'm encountering here.
Update: I seem to have worked around the issue by setting the title asynchronously:
_tabCell = loadTopNibObject(#"MYTabSmall3HeaderCell");
dispatch_async(dispatch_get_main_queue(), ^{
_tabCell.tab0Button.normalTitle = #"Option 1";
_tabCell.tab1Button.normalTitle = #"Option 2";
_tabCell.tab2Button.normalTitle = #"Option 3";
});
Update 2: Nope, the above doesn't work. I've breakpointed and printed out the empty button's text, which reports the correct string even though the button appears blank. I've also tried running setNeedsDisplay: when the button is touched, to no avail. Also tried re-setting the button text to some specific string (e.g. #"test") on touch but this doesn't kick in any visible letting.

How to display tableview images as soon as they are downloaded rather than after reloading cells

I have a table view that displays a list of users. The avatar images in each cell are downloaded asynchronously using UIImageView+AFNetworking and displayed using UIImage+TPAdditions. Here is my code snippet, from cellForRowAtIndexPath:
if (cell==nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SimpleTableIdentifier];
}
cell.textLabel.text = [[global.people objectAtIndex:indexPath.row]name];
// setImageWithUrl:placeholderImage: is taken from UIImageView+AFNetworking
NSString* imgURL = [[global.people objectAtIndex:indexPath.row]avatar_url];
UIImageView* imgV = [[UIImageView alloc]init];
[imgV setImageWithURL:[NSURL URLWithString:imgURL] placeholderImage:[UIImage imageNamed:#"icon.png"]];
//These are just for formatting and are taken from UIImage+TPAdditions
cell.imageView.image = [imgV.image imageScaledToSize:CGSizeMake(43,43)];
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 5.0;
Now when the app loads we just see the placeholder images until the user scrolls the tableview up and down. Then the avatar images from the URLs are loaded into each cell's image view. I want to make it so that this scrolling is not required- I want the avatar images to "pop" into their respective cells' tableviews as soon as they are downloaded. I know NSNotification might help me, but I'm not really sure where/how to use that. I'm fairly new to iOS. Can anyone walk me through it?
Thanks
You'll want to call setNeedsDisplay on your view to force a refresh. You would call this in your asynchronous call's completion handler.