我正在制作一个小照片编辑应用程序以获得乐趣。用户必须从相机胶卷中选择一张照片,然后将其导入进行修改。
这通常如何运作?我见过很多应用程序允许使用标准控制器看起来总是一样的。
是否也可以直接访问该库或自定义该控制器的外观?
我应该从哪里开始寻找?
答案 0 :(得分:21)
最简单的方法是在简单的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.presentViewController(picker, animated: true, completion: nil)
}))
alert.addAction(UIAlertAction(title: "Photo Library", style: .Default, handler: {
action in
picker.sourceType = .PhotoLibrary
self.presentViewController(picker, animated: true, completion: nil)
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
然后只需添加委托就可以了。
extension ProfileViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
//use image here!
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
}
很抱歉,这个例子很快,但我希望它仍有帮助。
答案 1 :(得分:9)
我致力于一个允许用户选择个人形象的应用程序。我有两个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{}
希望它有所帮助! ;)
答案 2 :(得分:6)
此答案仅适用于物理设备!
访问摄像头:
- (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
的更多信息答案 3 :(得分:5)
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
答案 4 :(得分:2)
答案 5 :(得分:1)
这是一个示例应用程序和一个包装器,可以像Facebook一样为您提供拍照或从库中选择。 https://github.com/fulldecent/FDTake
答案 6 :(得分:1)
这将进入视图控制器类块,其中包含要触发相机/图像选择器的按钮。
@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)
}
这低于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)
}
}