如何允许用户从他的相机胶卷或照片库中挑选照片?

Posted

技术标签:

【中文标题】如何允许用户从他的相机胶卷或照片库中挑选照片?【英文标题】:How to allow the user to pick a photo from his camera roll or photo library? 【发布时间】:2011-05-24 12:34:29 【问题描述】:

我正在制作一个有趣的照片编辑应用程序。用户必须从他们的相机胶卷中选择一张照片,然后将其导入以进行修改。

这通常是如何工作的?我见过许多应用程序允许使用看起来始终相同的标准控制器。

是否也可以直接访问该库或自定义该控制器的外观?

我应该从哪里开始寻找?

【问题讨论】:

官方文档:developer.apple.com/library/content/documentation/AudioVideo/… 【参考方案1】:

最简单的方法是在简单的 alertView 中使用 UIImagePickerController。

例如,您希望某人点击他们的个人资料照片,并能够从他们的相机或照片库中设置新图像。

@IBAction func btnProfilePicTap(sender: AnyObject) 
    let picker = UIImagePickerController()
    picker.delegate = self
    let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: 
        action in
        picker.sourceType = .camera
        self.present(picker, animated: true, completion: nil)
    ))
    alert.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: 
        action in
        picker.sourceType = .photoLibrary
        self.present(picker, animated: true, completion: nil)
    ))
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    self.present(alert, animated: true, completion: nil)

然后只需添加代理即可。

extension ProfileViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate 
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) 
        //use image here!
        dismiss(animated: true, completion: nil)
    

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) 
        dismiss(animated: true, completion: nil)
    

对不起,这个例子很快,但我希望它仍然有帮助。

编辑:为 Swift 5 更新。

【讨论】:

RSKImageCropper 的 Swift 实现。 github.com/Sushobhitbuiltbyblank/…【参考方案2】:

我开发了一个允许用户选择个人图像的应用程序。我有两个 UIButtons 可以帮助用户选择图片,无论是来自相机还是图书馆。是这样的:

- (void)camera 
if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    return;

UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//Permetto la modifica delle foto
picker.allowsEditing = YES;
//Imposto il delegato
[picker setDelegate:self];

[self presentModalViewController:picker animated:YES];

- (void)library 
//Inizializzo la classe per la gestione della libreria immagine
UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//Permetto la modifica delle foto
picker.allowsEditing = YES;
//Imposto il delegato
[picker setDelegate:self];

[self presentModalViewController:picker animated:YES];

你必须实现 UIImagePickerControllerDelegate:

@interface PickPictureViewController : UIViewController <UIImagePickerControllerDelegate>

@implementation PickPictureViewController

#pragma mark UIImagePickerController Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
UIImage *pickedImage = [info objectForKey:UIImagePickerControllerEditedImage];
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) 
    UIImageWriteToSavedPhotosAlbum(pickedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

[self dismissModalViewControllerAnimated:YES];

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
[self dismissModalViewControllerAnimated:YES];

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo

希望对您有所帮助! ;)

【讨论】:

【参考方案3】:

此答案仅适用于物理设备!

访问相机:

- (void)takePhoto 

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;

[self presentViewController:picker animated:YES completion:NULL];


访问相机胶卷:

- (void)selectPhoto 

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

[self presentViewController:picker animated:YES completion:NULL];



实现 UIImagePickerController 的委托方法:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;

[picker dismissViewControllerAnimated:YES completion:NULL];


还有这个:

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 

[picker dismissViewControllerAnimated:YES completion:NULL];


还可以查看link的更多信息

【讨论】:

【参考方案4】:

SWIFT 2.0

感谢 William T。这对我的 UITapGestureRecognizer 有用

func selectPhoto(tap: UITapGestureRecognizer) 
    let picker = UIImagePickerController()
    picker.delegate = self
    let alert = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
    alert.addAction(UIAlertAction(title: "Camera", style: .Default, handler: 
        action in
        picker.sourceType = .Camera
        picker.allowsEditing = true
        self.presentViewController(picker, animated: true, completion: nil)
    ))
    alert.addAction(UIAlertAction(title: "Photo Library", style: .Default, handler: 
        action in
        picker.sourceType = .PhotoLibrary
        picker.allowsEditing = true
        self.presentViewController(picker, animated: true, completion: nil)
    ))
    alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)

我添加了以下内容,以便在将照片同时选择到 .Camera 和 .PhotoLibrary 后对其进行编辑:

picker.allowsEditing = true

【讨论】:

【参考方案5】:

看看UIImagePickerController

【讨论】:

【参考方案6】:

@WilliamT 的回答对我来说非常有效。这是他的,但已针对 Swift 4 进行了更新,以防有人仍在寻找它。

这将进入包含您要触发相机/图像选择器的按钮的视图控制器类块。

@IBAction func YourButtonToTriggerCamera/ImagePicker(_ sender: UIButton) 
    let picker = UIImagePickerController()
    picker.delegate = (self as UIImagePickerControllerDelegate & UINavigationControllerDelegate)
    let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: 
        action in
        picker.sourceType = .camera
        self.present(picker, animated: true, completion: nil)
    ))
    alert.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: 
        action in
        picker.sourceType = .photoLibrary
        self.present(picker, animated: true, completion: nil)
    ))
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    self.present(alert, animated: true, completion: nil)

这在你的视图控制器类下面:

extension YourViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate 

    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) 
        //use image here!
        dismiss(animated: true, completion: nil)
    

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) 
        dismiss(animated: true, completion: nil)
    

【讨论】:

【参考方案7】:

这是一个示例应用程序和一个包装器,可以像 Facebook 一样为您提供拍照或从库中选择。 https://github.com/fulldecent/FDTake

【讨论】:

以上是关于如何允许用户从他的相机胶卷或照片库中挑选照片?的主要内容,如果未能解决你的问题,请参考以下文章

Phonegap:显示相机胶卷中的照片列表?

Android 让用户从图库或相机中挑选/捕捉照片

从 ipad 上的相机胶卷中选择照片需要弹出框控制器错误

科尔多瓦,从相机胶卷中获取所有照片

我想让用户选择使用相机拍照或从库中挑选照片(ios-swift,apple example-foodtracker)

重启ios应用后检查照片是不是仍然存在于相机胶卷中