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

发布于 2024-11-09 13:25:38 字数 153 浏览 1 评论 0原文

我正在制作一个小照片编辑应用程序来娱乐。用户必须从相机胶卷中选择一张照片,然后将其导入进行修改。

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

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

我应该从哪里开始寻找?

I'm making a little photo editing app for fun. Users must select a photo from their camera roll which will then be imported for modification.

How does this generally work? I have seen many apps allowing this with a standard controller that looks always the same.

Is it also possible to access this library directly or to customize the appearance of that controller?

Where should I start looking?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

栀梦 2024-11-16 13:25:38

最简单的方法是在简单的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 进行了更新。

The easiest way to do this is by using the UIImagePickerController in a simple alertView.

For example, you want someone to tap on their profile picture and be able to set a new image from either their camera or their photo library.

enter image description here

@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)
}

Then simply add the delegate and you're done.

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)
    }
}

Sorry, this example is in swift but I hope it still helps.

EDIT: Updated for Swift 5.

就是爱搞怪 2024-11-16 13:25:38

我开发了一个允许用户选择个人图像的应用程序。我有两个 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{}

希望它有帮助! ;)

I worked on an application that allows user to select a personal image. I had two UIButtons which could help the user to pick a picture, whether it was from camera or library. It's something like this:

- (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];
}

You have to implement the 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{}

Hope it helps! ;)

执妄 2024-11-16 13:25:38

该答案仅与物理设备相关!

访问相机:

- (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];

}

另请检查有关此的更多信息 链接

This answer relevant on physical device ONLY!

Access Camera:

- (void)takePhoto {

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

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

}

Access Camera Roll:

- (void)selectPhoto {

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

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


}

Implementing the Delegate Methods of UIImagePickerController:

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

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

[picker dismissViewControllerAnimated:YES completion:NULL];

}

And This:

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

[picker dismissViewControllerAnimated:YES completion:NULL];

}

Also check for more info on this link

吾性傲以野 2024-11-16 13:25:38

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

SWIFT 2.0

Thanks to William T. this worked for my on my 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)
}

I added the following to let me edit the photo after selecting it to both the .Camera and .PhotoLibrary:

picker.allowsEditing = true
胡渣熟男 2024-11-16 13:25:38

@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)
    }
}

@WilliamT 's answer worked really well for me. Here is his, but updated for Swift 4 in case anyone is still looking for this.

This goes into the view controller class block that contains the button that you want to trigger the camera/image picker.

@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)
}

This goes below your View Controller Class:

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)
    }
}
神经暖 2024-11-16 13:25:38

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

Here is an example app and a wrapper that gives you the Take Photo or Select from Library just like Facebook does. https://github.com/fulldecent/FDTake

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文