1

私は現在 と を持っていUIImageViewますUIImage。ユーザーがカメラ ロールから写真を選択できるようにするにはどうすればよいですか?

重複の可能性 がありますが、運がありませんでした。これは、私が Interface Builder に接続していないことが原因である可能性があります。

のような単純なものが欲しいのですUIImage * image = [UIImage imageFromCameraRoll]が、残念ながらそれは不可能です (少なくともそうは思いません)。

4

2 に答える 2

3

このコードを試してください

cameraroll&から画像を取得するのに役立ちますphotolibrary。使用してUIImagePickerViewController

写真を撮れば

  1. カメラから (どちらも UINavigationcontroller として開いています)。
  2. ギャラリーから (iPad の場合は UIpopovercontroller として開き、iPhone の場合は nvigationcontroller として開きます)。

最初のセットのデリゲート

@interface camera : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate,UIPopoverControllerDelegate>

カメラから画像を取得する

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            UIImagePickerController *imagePicker =
            [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            imagePicker.sourceType =
            UIImagePickerControllerSourceTypeCamera;
            imagePicker.mediaTypes = [NSArray arrayWithObjects:
                                      (NSString *) kUTTypeImage,
                                      nil];

            imagePicker.allowsEditing = YES;

            imagePicker.wantsFullScreenLayout = YES;

            [self presentViewController:imagePicker animated:YES completion:nil];
            newMedia = YES;
            iscamera = 0;
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error to access Camera"
                                                            message:@""
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];

        }

ギャラリーから画像を取得

 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        UIImagePickerController *_picker=nil;
        if (popoverController) {
            [popoverController dismissPopoverAnimated:NO];

        }
        _picker = [[UIImagePickerController alloc] init];
        _picker.delegate = self;        
        _picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        _picker.wantsFullScreenLayout = YES;

        //[popoverController presentPopoverFromBarButtonItem:sender
                               //   permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

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


        } else
        {
            popoverController = [[UIPopoverController alloc] initWithContentViewController:_picker];
            [popoverController setDelegate:self];
            [popoverController presentPopoverFromRect:btn.frame
                                               inView:self.view
                             permittedArrowDirections:UIPopoverArrowDirectionLeft
                                             animated:YES];
        }
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error access photo library"
                                                        message:@"your device non support photo library"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];

    }

Delegate メソッドで取得した両方の結果

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   // write your code here ........
 }
于 2013-01-14T22:35:17.847 に答える
1

を使用UIImagePickerControllerしてください。Appleドキュメントを参照してください。

ソースタイプを選択します。

@property (nonatomic) UIImagePickerControllerSourceType sourceType

これら以降:

UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum
于 2013-01-14T22:29:01.387 に答える