簡體   English   中英

如何允許用戶從他的相機膠卷或照片庫中挑選照片?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM