UIButton图像未更改/更新 [英] UIButton Image not changing/updating

查看:32
本文介绍了UIButton图像未更改/更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我对Objective-C还是陌生的,并且仍在尝试学习尽可能多的东西,所以请多多包涵.

First off I am still new to objective-c and still trying to learn as much as I can so please bear with me.

现在,我有一个以编程方式创建的UIButton.当按下按钮时,将显示UIActionSheet,其中包括相机",选择照片"或取消".然后假定按钮图像更改为拍摄的图像(如果选择了相机)或拍摄的图像(如果选择了相机胶卷).

Right now I have a UIButton that I've created programmatically. When the button is pressed an UIActionSheet is brought up with the choice of "Camera," "Choose Photo" or "Cancel." The button image is then suppose to change to the image taken (if camera was chosen) or image picked (if the camera roll was chosen).

我的问题是在关闭UIImagePicker后按钮的图像没有更改.最初,按钮是作为tableView的子视图创建的,当我滚动tableView时,它刷新了按钮,并且图像发生了变化(这当然不是我希望的行为).但是,如果用户决定要更改或删除图像,则只需再次按下按钮,UIActionSheet就会显示删除照片"的第三种选择.滚动tableView之后,我意识到按钮图像根本没有改变,但是在旧按钮的顶部创建了一个新按钮,因此我将UIButton代码移至viewDidLoad(基于在stackoverflow上找到的一些信息).我也删除了tableView滚动的功能,因为我不需要滚动它.

My problem is the button's image is not changing after UIImagePicker is dismissed. At first the button was created as a subView of a tableView and when I scrolled the tableView, it refreshed the button and the image changed (this is not how I wanted it to behave of course). However if the user decides they want to change or remove the image, they just push the button again and the UIActionSheet displays a third choice to "Remove Photo." After scrolling the tableView I realized the button image was not changing at all, but instead a new button was created on top of the old button so I move the UIButton code to viewDidLoad instead (based upon some information found here on stackoverflow). I have also removed the ability for the tableView to scroll as I did not need scrolling for it.

几天来,我一直在尝试解决此问题,如何在关闭UIImagePicker或UIActionSheet后刷新按钮或视图.我曾尝试使用setNeedsLayout和setNeedsDisplay,但这些方法无法解决我的问题(我可能将它们放在错误的位置).希望有人对此有所了解,并可以指导我完成这项工作的正确方法.还提供了我的代码:

I've been trying to solve this problem for a few days now how to refresh either the button, or the view properly after either UIImagePicker is dismissed or UIActionSheet is dismissed. I have tried using setNeedsLayout and setNeedsDisplay but these have not worked to fix my problem (I might be putting these in the wrong place). Hopefully someone has insight on this and can guide me on the proper way to make this work. Also provided is my code:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // Creates camera button and connects to camera menu UIActionSheet
    UIButton *cameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cameraButton addTarget:self action:@selector(showActionSheet:) forControlEvents:UIControlEventTouchUpInside];
    cameraButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
    cameraButton.titleLabel.textAlignment = UITextAlignmentCenter;
    cameraButton.frame = CGRectMake(9, 230, 80, 80);
    [self.view addSubview:cameraButton];

    picturePresent = NO;
    NSLog(@"picturePresent = %@", picturePresent);
}

-(void)showActionSheet:(id)sender {
    if (picturePresent == NO) {
    UIActionSheet *cameraMenuSheet = [[UIActionSheet alloc] initWithTitle:@"Add Photo" 
                                                                 delegate:self 
                                                        cancelButtonTitle:@"Cancel" 
                                                   destructiveButtonTitle:nil 
                                                        otherButtonTitles:@"Camera", @"Choose Photo", nil];
    [cameraMenuSheet showFromTabBar:self.tabBarController.tabBar];
    }
    else if (picturePresent == YES) {
    UIActionSheet *cameraMenuSheet = [[UIActionSheet alloc] initWithTitle:@"Add Photo" 
                                                                 delegate:self 
                                                        cancelButtonTitle:@"Cancel" 
                                                   destructiveButtonTitle:nil 
                                                        otherButtonTitles:@"Remove Photo", @"Camera", @"Choose Photo", nil];
    [cameraMenuSheet showFromTabBar:self.tabBarController.tabBar];
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex];

    // Initialize UIImagePickerController
    if ([title isEqualToString:@"Camera"]) {
        // Camera
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.delegate = self;
        imagePicker.allowsEditing = NO;
        imagePicker.showsCameraControls = YES;
        imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];

        [self presentModalViewController:imagePicker animated:YES];
        newMedia = YES;
    }
    else if ([title isEqualToString:@"Choose Photo"]) {
        // Photo library
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        imagePicker.delegate = self;
        imagePicker.allowsEditing = NO;
        imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
        [self presentModalViewController:imagePicker animated:YES];
        newMedia = NO;
    }
    else if ([title isEqualToString:@"Remove Photo"]) {
    picturePresent = NO;

    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    [self.view setNeedsDisplay];
    }

// Method for saving image to photo album
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {

    // Access the uncropped image from info dictionary
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    UIImage *scaledImage = [image scaleToSize:CGSizeMake(80.0, 80.0)];

        if (newMedia == YES) {
        // Save image
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

        }

        resizedImage = scaledImage;
    }

    picturePresent = YES;
    [self dismissModalViewControllerAnimated:NO];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissModalViewControllerAnimated:YES];
}

另外,现在我已经将UIButton编码移到了viewDidLoad上,我不确定将这段编码放在哪里:

Additionally, now that I have moved my UIButton coding to viewDidLoad I am unsure of where to put this piece of coding:

{
    if (picturePresent == NO) {

        [cameraButton setTitle:@"Add\nPhoto" forState:UIControlStateNormal];
    }

    else if (picturePresent == YES) {

        [cameraButton setImage:resizedImage forState:UIControlStateNormal];
    }
}

还使用了setImage和setBackgroundImage,但仍然无法正常工作.让我知道是否需要更多信息,谢谢!

Also setImage and setBackgroundImage were both used and still not working how I want it to. Let me know if more information is needed, thanks!

以下是一些屏幕截图,以显示我的目标-

Here are some screenshots to show what I am aiming for -

假设图片"代表拍摄或选择的图片,然后缩放以适合按钮大小.

"Picture" is suppose to represent a picture that was taken or selected and then scaled to fit the button size.

推荐答案

没有测试它,但是如果您将cameraButtoncameraButton成员设置为可以在需要时访问,则可以调用在计算resizedImage之后-无需picturePresent bool.而且我认为setNeedsDisplay也不需要.

Did not test it, but if you make your cameraButton member of your UiViewController so that you can access whenever needed, you could call the setImage: forState: right after caculating the resizedImage - no picturePresent bool needed. And I think setNeedsDisplay Not needed as well.

在视图控制器头文件中执行类似的操作

In your view controller header file do something like

...
@interface MyViewController : UIViewController
{
   ...
   UIButton * cameraButton;
   ...
}
... 

在实现文件中的某个位置(viewDidiLoad可以)执行以下操作:

Somewhere in your implementation file (viewDidiLoad is Ok) do:

...
cameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
...    
[self.view addSubview:cameraButton];
...

然后实现您所做的所有其他工作,并像这样更改didFinishPickingMediaWithInfo中的代码:

And then implement all the other stuff you did and change the code in didFinishPickingMediaWithInfo like this:

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{

   ...
  [cameraButton  scaledImage forState:UIControlStateNormal];
}

正如我所说,没有测试它,但是我认为它应该可以工作.

As I said, didn't test it, but I think it should work.

这篇关于UIButton图像未更改/更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆