首先,我对 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 ,但仍然无法按照我想要的方式工作。如果需要更多信息,请告诉我,谢谢!
编辑:
以下是一些屏幕截图,以显示我的目标 -
假设“图片”代表拍摄或选择的图片,然后缩放以适合按钮大小。