-3

如何在代码中创建图像选择器?

我使用带有 ARC 的 iOS 6.0。

我希望能够选择图片并以某种方式获取所选图像的 UIImage 。

4

3 回答 3

3

基本实例化是这样的

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setDelegate:self];
imagePicker.modalPresentationStyle = UIModalPresentationOverFullScreen;
[self presentViewController:imagePicker animated:YES completion:nil];

获取图片的三种方式

//Pick from Camera
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;//Make sure to enable permission for this in info.plist; see: https://stackoverflow.com/a/44690117/2057171

//Pick from all folders in the gallery
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

//Pick from PhotosAlbum(camera roll)
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

然后检查源是否存在

//Check Camera available or not
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

//Check PhotoLibrary available or not
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) 

//Check front Camera available or not
if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront])

您应该实现委托协议以获取用户选择的图像

//Tells the delegate that the user picked a still image or movie.
 (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
  UIImage *selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
  [imagepicker dismissModalViewControllerAnimated:YES];
}

//Tells the delegate that the user cancelled the pick operation.
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
}

使用 UIImagePickerController 的简单教程

http://www.techotopia.com/index.php/An_Example_iOS_4_iPad_Camera_and_UIImagePickerController_Application_(Xcode_4)

于 2013-01-22T19:54:52.977 回答
1

<UIImagePickerControllerDelegate>将委托添加到文件@interface ViewController中的行.h

如果您要将模拟器用作调试器,请确保您的模拟器中有图片,然后调用以下方法

- (void)showImagePicker
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [self presentModalViewController:imagePicker animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // Get the selected image.
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    [picker dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissModalViewControllerAnimated:YES];
}
于 2013-01-22T19:52:27.857 回答
0

展示它:

UIImagePickerController *picker;
picker = [[UIImagePickerController alloc] init];
[picker setDelegate:self];
[picker setSourceType:type];

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

获取 UIImage:

- (void)imagePickerController:(UIImagePickerController *)thePicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{   
    // Do something with "image"
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    // users typically expect that if they took a photo it will be saved
    if (thePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }

}
于 2013-01-22T19:50:31.573 回答