3

首先,我对 Objective-c 还是很陌生,并且仍在尝试尽可能多地学习,所以请多多包涵。

现在我有一个以编程方式创建的 UIButton。当按下按钮时,会弹出一个 UIActionSheet,其中包含“相机”、“选择照片”或“取消”选项。然后假设按钮图像更改为拍摄的图像(如果选择了相机)或选择的图像(如果选择了相机胶卷)。

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

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

- (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,我不确定将这段编码放在哪里:

{
    if (picturePresent == NO) {

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

    else if (picturePresent == YES) {

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

还使用了 setImage 和 setBackgroundImage ,但仍然无法按照我想要的方式工作。如果需要更多信息,请告诉我,谢谢!

编辑:

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

屏幕1

屏幕2

屏幕3

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

4

3 回答 3

1

viewDidLoad代码移至imagePickerController:didFinishPickingMediaWithInfo:. 此外,如果您可以发布一些屏幕截图,它将有助于准确解释正在发生的事情以及您的期望。这可能是一个裁剪问题,这就是我建议发布屏幕截图的原因。

于 2012-04-04T21:10:24.650 回答
1

没有测试它,但如果你让你的cameraButton成员成为你的成员,UiViewController以便你可以在需要时访问,你可以在计算 -setImage: forState:后调用正确的resizedImage- 不需要picturePresentbool。我认为setNeedsDisplay也不需要。

编辑:

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

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

在您的实现文件中的某处(viewDidiLoad 可以)执行以下操作:

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

然后实现你所做的所有其他事情并didFinishPickingMediaWithInfo像这样更改代码:

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

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

正如我所说,没有测试它,但我认为它应该工作。

于 2012-04-04T21:27:58.260 回答
0

很长一段时间我一直在寻找答案..现在这里有一些:

要更改按钮的图像,下面的代码帮助了我:

[button.imageView setHighlighted:YES];
[button.imageView setHighlightedImage:[UIImage imageNamed:@"mypic.png"]];
[button.imageView setImage:[UIImage imageNamed:@"mypic.png"]];

..例如在 viewDidLoad 中。

希望有帮助...

于 2012-05-31T00:13:18.807 回答