首先,我對objective-c仍然很陌生,仍然試圖儘可能地學習,所以請耐心等待。UIButton圖像不變/正在更新
現在我有一個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均使用和仍然不工作,我想如何。如果需要更多信息,請告知我,謝謝!
編輯:
這裏有一些截圖來展示一下我的目標爲 -
「圖片」 是想表示圖片這是採取或選擇,然後縮放以適應按鈕的大小。
你能解釋一下你是什麼意思嗎?使它成爲UIViewController的成員。對不起,我還是iOS和obj-c編程的新手。 – fresh 2012-04-04 21:44:39
明天我會。目前正在打電話。 – 2012-04-04 22:19:08
經過一週的試圖繞過這個問題我的頭你的代碼做了伎倆。感謝這樣一個簡單但很好的解決方案。 – fresh 2012-04-05 22:37:31