[英]How to allow the user to pick a photo from his camera roll or photo library?
我正在制作一个有趣的照片编辑应用程序。 用户必须从他们的相机胶卷中 select 一张照片,然后将其导入以进行修改。
这通常如何工作? 我已经看到许多应用程序允许使用看起来总是相同的标准 controller 来实现这一点。
是否也可以直接访问该库或自定义该 controller 的外观?
我应该从哪里开始寻找?
最简单的方法是在一个简单的 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 但我希望它仍然有帮助。
编辑:为 Swift 5 更新。
我在一个允许用户 select 个人图像的应用程序上工作。 我有两个 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{}
希望能帮助到你; ;)
此答案仅与物理设备相关!
访问相机:
- (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];
}
另请查看有关此链接的更多信息
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
@WilliamT 的回答对我来说非常有效。 这是他的,但更新为 Swift 4,以防有人还在寻找这个。
这将进入视图 controller class 块,其中包含您要触发相机/图像选择器的按钮。
@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)
}
这低于您的视图 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)
}
}
这是一个示例应用程序和一个包装器,它为您提供库中的 Take Photo 或 Select,就像 Facebook 一样。 https://github.com/fulldecent/FDtake
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.