在Swift中使用UIButton制作图像选择器

时间:2015-10-22 13:02:38

标签: ios swift uiimagepickercontroller uistoryboard

我想制作一个带有指向ImagePickerView的UIButton的图像选择器,但我不知道如何继续。

我们要说我想连接ViewController" A" (使用UIButton)到ViewController" B" (UIImagePicker)。

这就是我尝试过的:我有一个带有多个ViewController的故事板,我不知道为什么我不能通过ViewController制作一个IBAction" A"到ViewController" B"。

我应该只以编程方式制作ImagePickerView还是可以使用Storyboard?

提前致谢!

2 个答案:

答案 0 :(得分:2)

回答我自己,这就是我在swift中所做的事情,并且有效(帮助其他人)

var currentImage: UIImage!

@IBAction func ImagePicker(sender: UIButton)
{
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary)
    {
        let picker = UIImagePickerController()
        picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        picker.delegate = self
        picker.allowsEditing = true
        self.presentViewController(picker, animated: true, completion: nil)
    }

}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    var newImage: UIImage

    if let possibleImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
        newImage = possibleImage
    } else if let possibleImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
        newImage = possibleImage
    } else {
        return
    }

    dismissViewControllerAnimated(true, completion: nil)

    currentImage = newImage
}

答案 1 :(得分:0)

无法在故事板中添加

UIImagePickerController,因此您必须以编程方式执行此操作。首先,使用您的UIButton创建一个IBAction:

- (IBAction)openImagePicker:(id)sender;

您的班级需要采用UIImagePickerControllerDelegateUINavigationControllerDelegate协议:

@interface MyViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

然后以openImagePicker:方法显示图片选择器:

// Check if image source is available
// In this case, photo library is always available. If you use
// UIImagePickerControllerSourceTypeCamera instead, simulator doesn't have it
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init]; // Create an image picker
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // Choose source type
    picker.delegate = self; // Delegate when image is picked
    picker.allowsEditing = YES; // Allow user to edit image before choose it
    [self presentViewController:picker animated:YES completion:nil]; // Show image picker
}

要使用已挑选的图片,您必须实施UIImagePickerControllerDelegate

的此方法
/**
 * @param picker The image picker
 * @param info Dictionary contains picked images and metadata...
 */
- (void)imagePickerController:(nonnull UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(nonnull NSDictionary *)info {
    // Because we allow user to edit image,
    // so we choose UIImagePickerControllerEditedImage
    // You can see other keys in UIImagePickerController class header
    UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];

    // Dismiss the picker
    dispatch_async(dispatch_get_main_queue(), ^{
        [self dismissViewControllerAnimated:YES completion:nil];
    });

    // Use the image here
}