我的视图控制器上有3个图像视图。我有一个选择,如选择画廊或拍照。现在,我想要的是相机。我想默认选择图像,我们可以选择一个,但我想要三个我们如何实现它。
答案 0 :(得分:2)
使用Tag
概念
<强>步骤1 强>
最初将每个imageView的标记指定为1,2,3
,然后创建一个打开UIImagePickerController
的常用方法
<强>步骤-2 强>
为每张图片创建手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageTap:)];
tap.cancelsTouchesInView = YES;
tap.numberOfTapsRequired = 1;
tap.delegate = self;
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:tap];
调用方法如
- (IBAction)handleImageTap:(UIGestureRecognizer*)sender
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Attach image" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* pickFromGallery = [UIAlertAction actionWithTitle:@"Take a photo"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.view.tag = sender.view.tag;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:NULL];
}
}];
UIAlertAction* takeAPicture = [UIAlertAction actionWithTitle:@"Choose from gallery"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.view.tag = sender.view.tag;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:NULL];
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
}];
[alertController addAction:pickFromGallery];
[alertController addAction:takeAPicture];
[alertController addAction:cancel];
[self presentViewController:alertController animated:YES completion:NULL];
}
<强>步骤-3 强>
关于该委托方法的
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
if(picker.view.tag == 1)
{
// assign the image to first
}
else if(picker.view.tag == 2)
{
// assign the image to second
}
else
{
// assign the image to third
}
}
答案 1 :(得分:0)
您可以编写自己的逻辑,例如
在didFinishPickingImage
递增一个计数器,并且在计数器达到3之前不要忽略image picker
控制,并且每次在文档目录或临时目录中存储本地存储时都存储图像。
请参阅this so post的已接受答案,以证明我所解释的内容。
如果您想使用第三方库,则ELCImagePickerController 是更好的解决方案。
答案 2 :(得分:0)