不说话,直接上个例子,很简单哦
@interfaceViewController ()
@end
@implementation ViewController {
UIAlertController * actionSheet;
UIImageView *imageview;
}
- (void)viewDidLoad {
[superviewDidLoad];
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 220, 100, 40);
[btn setTitle:@"获取"forState:0];
[btn setTitleColor:[UIColor greenColor] forState:0];
[self.view addSubview:btn];
[btn addTarget:selfaction:@selector(BtnClick) forControlEvents:UIControlEventTouchUpInside];
actionSheet =[UIAlertControlleralertControllerWithTitle:@"选择图像"message:nilpreferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *num1=[UIAlertActionactionWithTitle:@"相册"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction * _Nonnull action) {
NSLog(@"11111");
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[selfpresentViewController:imagePickerController animated:YEScompletion:^{ NSLog(@"进入相册");
}];
}];
UIAlertAction *num2=[UIAlertActionactionWithTitle:@"拍照"style:0handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"222222");
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ) {
UIImagePickerController *imagePickerController = [[UIImagePickerControlleralloc] init];
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES;
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[selfpresentViewController:imagePickerController animated:YEScompletion:^{ NSLog(@"进入相机");
}];
}else {
UIAlertView *aa=[[UIAlertViewalloc] initWithTitle:@"温馨提示"message:@"操作警告"delegate:selfcancelButtonTitle:nilotherButtonTitles:@"不支持相机", nil];
[aa show];
} }];
UIAlertAction *num3=[UIAlertActionactionWithTitle:@"取消"style:1handler:^(UIAlertAction * _Nonnull action) { NSLog(@"333333");
}];
[actionSheetaddAction:num1];
[actionSheetaddAction:num2];
[actionSheetaddAction:num3];
imageview = [[UIImageViewalloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
imageview.image = nil; [self.viewaddSubview:imageview];
}
- (void)BtnClick {
[selfpresentViewController:actionSheetanimated:YEScompletion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YEScompletion:^{ }];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSLog(@"image:%@",image);
imageview.image = image;
}
ok了,复制一下没多少代码的。。。。。。求赞