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

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

问题描述:

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

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?

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

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

您必须实现UIImagePickerControllerDelegate:

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

希望它有帮助! ;)