【发布时间】:2014-07-11 18:19:52
【问题描述】:
我有一个非常奇怪的问题:我有一个 TableViewController 有 2 个图片和 2 个按钮。用户可以使用按钮更改图片。
问题是当用户单击按钮时,UIActionSheet 打开,用户选择“照片来自库”,然后在库中选择一张照片。 UIActionSheet 关闭,但我的 ImageView 中没有图片...它仍然是空白的...(我的 ImageView 链接到 @ 属性)。
这是我的代码:
.H 文件:
#import <UIKit/UIKit.h>
@interface EditUserTableViewController : UITableViewController <UITextFieldDelegate, UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *avatar;
@property (weak, nonatomic) IBOutlet UIImageView *coverPhoto;
- (IBAction)changeImage:(id)sender;
@end
.M 文件:
#import "EditUserTableViewController.h"
typedef NS_ENUM(NSInteger, THPhotoType)
{
THPhotoTypeCoverPhoto = 10,
THPhotoTypeAvatarPhoto = 20
};
@interface EditUserTableViewController ()
@property (nonatomic, assign) THPhotoType selectedPhotoType;
@end
@implementation EditUserTableViewController
@synthesize coverPhoto, avatar;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)changeImage:(id)sender {
if ([sender isKindOfClass:[UIButton class]]) {
UIButton *button = sender;
_selectedPhotoType = button.tag;
}
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Select Picture" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"From Library", @"From Camera", nil];
[sheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex < 2) {
UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
UIImagePickerControllerSourceType type;
switch (buttonIndex) {
case 0:
type = UIImagePickerControllerSourceTypePhotoLibrary;
break;
case 1:
type = UIImagePickerControllerSourceTypeCamera;
break;
default:
break;
}
imagePicker.sourceType = type;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:^{ }];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
switch (_selectedPhotoType) {
case THPhotoTypeCoverPhoto:
coverPhoto.image = [info objectForKey:UIImagePickerControllerOriginalImage];
break;
case THPhotoTypeAvatarPhoto:
avatar.image = [info objectForKey:UIImagePickerControllerOriginalImage];
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:^{ }];
}
@end
【问题讨论】:
标签: ios uitableview uiimageview uiimagepickercontroller